1

I am writing a specflow test in C#, how can I pass future date or less than today's date or today's date in spec flow ? I can send DATE something like 2018-05-01 but looking for something like current date - 1 year, or - 1 months or 1+ day etc.

Below works fine,

And the following user data
|User_ID | Start_Date | End_Date|
|ABCX123 |2018-05-01  |2018-05-30|

but I want to pass End_Date as Today's date + 1 year and Start Date as Today's date - 1 month

Can we use date functions ?

1
  • The current date/time is an external dependency. You should mock it so that your test can provide a reliable value for repeatable testing. Commented May 2, 2018 at 17:33

1 Answer 1

2

I'm afraid, Specflow provides this feature. I'd a similar requirement in past and we have used the below code (taken from here) which converts string to proper datetime.

  • next month, next year,
  • next 4 months, next 3 days
  • 3 days ago, 5 hours ago
  • tomorrow, yesterday
  • last year, last month,
  • last tue, next fri
  • last june, next may,
  • jan 2008, 01 january 2009,
  • june 2019, 2009/01/01

The helper class:

class FuzzyDateTime
{

    static List<string> dayList = new List<string>() { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
    static List<IDateTimePattern> parsers = new List<IDateTimePattern>()
    {
       new RegexDateTimePattern (
            @"next +([2-9]\d*) +months",
            delegate (Match m) {
                var val = int.Parse(m.Groups[1].Value); 
                return DateTime.Now.AddMonths(val);
            }
       ),
       new RegexDateTimePattern (
            @"next +month",
            delegate (Match m) { 
                return DateTime.Now.AddMonths(1);
            }
       ),           
       new RegexDateTimePattern (
            @"next +([2-9]\d*) +days",
            delegate (Match m) {
                var val = int.Parse(m.Groups[1].Value); 
                return DateTime.Now.AddDays(val);
            }
       ),

       new RegexDateTimePattern (
            @"([2-9]\d*) +months +ago",
            delegate (Match m) {
                var val = int.Parse(m.Groups[1].Value); 
                return DateTime.Now.AddMonths(-val);
            }
       ),
       new RegexDateTimePattern (
            @"([2-9]\d*) days +ago",
            delegate (Match m) {
                var val = int.Parse(m.Groups[1].Value); 
                return DateTime.Now.AddDays(-val);
            }
       ),
       new RegexDateTimePattern (
            @"([2-9]\d*) *h(ours)? +ago",
            delegate (Match m) {
                var val = int.Parse(m.Groups[1].Value); 
                return DateTime.Now.AddMonths(-val);
            }
       ),
       new RegexDateTimePattern (
            @"tomorrow",
            delegate (Match m) {
                return DateTime.Now.AddDays(1);
            }
       ),
       new RegexDateTimePattern (
            @"today",
            delegate (Match m) {
                return DateTime.Now;
            }
       ),
       new RegexDateTimePattern (
            @"yesterday",
            delegate (Match m) {
                return DateTime.Now.AddDays(-1);
            }
       ),
       new RegexDateTimePattern (
            @"(last|next) *(year|month)",
            delegate (Match m) {
                int direction = (m.Groups[1].Value == "last")? -1 :1;
                switch(m.Groups[2].Value) 
                {
                    case "year":
                        return new DateTime(DateTime.Now.Year+direction, 1,1);
                    case "month":
                        return new DateTime(DateTime.Now.Year, DateTime.Now.Month+direction, 1);
                }
                return DateTime.MinValue;
            }
       ),
       new RegexDateTimePattern (
            String.Format(@"(last|next) *({0}).*", String.Join("|", dayList.ToArray())), //handle weekdays
            delegate (Match m) {
                var val = m.Groups[2].Value;
                var direction = (m.Groups[1].Value == "last")? -1 :1;
                var dayOfWeek = dayList.IndexOf(val.Substring(0,3));
                if (dayOfWeek >= 0) {
                    var diff = direction*(dayOfWeek - (int)DateTime.Today.DayOfWeek);
                    if (diff <= 0 ) { 
                        diff = 7 + diff;
                    }
                    return DateTime.Today.AddDays(direction * diff);
                }
                return DateTime.MinValue;
            }
       ),

       new RegexDateTimePattern (
            @"(last|next) *(.+)", // to parse months using DateTime.TryParse
            delegate (Match m) {
                DateTime dt;
                int direction = (m.Groups[1].Value == "last")? -1 :1;
                var s = String.Format("{0} {1}",m.Groups[2].Value, DateTime.Now.Year + direction);
                if (DateTime.TryParse(s, out dt)) {
                    return dt;
                } else {
                    return DateTime.MinValue;
                }
            }
       ),
       new RegexDateTimePattern (
            @".*", //as final resort parse using DateTime.TryParse
            delegate (Match m) {
                DateTime dt;
                var s = m.Groups[0].Value;
                if (DateTime.TryParse(s, out dt)) {
                    return dt;
                } else {
                    return DateTime.MinValue;
                }
            }
       ),
    };

    public static DateTime Parse(string text)
    {
        text = text.Trim().ToLower();
        var dt = DateTime.Now;
        foreach (var parser in parsers)
        {
            dt = parser.Parse(text);
            if (dt != DateTime.MinValue)
                break;
        }
        return dt;
    }
}
interface IDateTimePattern
{
    DateTime Parse(string text);
}

class RegexDateTimePattern : IDateTimePattern
{
    public delegate DateTime Interpreter(Match m);
    protected Regex regEx;
    protected Interpreter inter;
    public RegexDateTimePattern(string re, Interpreter inter)
    {
        this.regEx = new Regex(re);
        this.inter = inter;
    }
    public DateTime Parse(string text)
    {
        var m = regEx.Match(text);

        if (m.Success)
        {
            return inter(m);
        }
        return DateTime.MinValue;
    }
}

Usage example:

var val = FuzzyDateTime.Parse(dateString);


And the following user data
|User_ID | Start_Date | End_Date|
|ABCX123 |tomorrow  |next month|
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.