C# 如何在要素文件中使用变量

C# 如何在要素文件中使用变量,c#,tdd,bdd,specflow,gherkin,C#,Tdd,Bdd,Specflow,Gherkin,如何在要素文件中使用变量?具体来说,我需要使用dateTime.now。理想情况下,类似于 Given the API returns items for "dateTime.now" when my function is run then I want that data in my database 在我的验收测试文件中 [Given("The API returns line items for (.*)")] 这样做对吗?我不确定如何在功能文件中使用变量。我希望我的验收测试使用当

如何在要素文件中使用变量?具体来说,我需要使用
dateTime.now
。理想情况下,类似于

Given the API returns items for "dateTime.now"
when my function is run 
then I want that data in my database
在我的验收测试文件中

[Given("The API returns line items for (.*)")]

这样做对吗?我不确定如何在功能文件中使用变量。我希望我的验收测试使用当前日期。

最简单的方法是编写一个特定于“立即”返回行项目的步骤:

您可以从新版本调用步骤的其他版本:

[给定(@“API立即返回项目”)]
public void GivenTheAPIReturnsItemsForRightNow()的
{
GivenTheAPIReturnsItemsFor(DateTime.Now);
}

这避免了步骤之间的代码重复。

我部分同意Greg Brughardts的答案。因为功能文件可以与业务涉众(或组织中的其他非IT人员)共享,所以在功能文件中使用“真实世界”语言更有意义。 此外,当您在测试结束时将其传递给报告工具时,它将更具可读性

我会这样做。switch语句使使用真实语言添加其他类型的日期变得容易:

[Given("The API returns line items for '(.*)'")]
public void GivenTheAPIReturnsItemsForTime(string mydate)
{
     switch (mydate)
     {
          case:"the current date":
              HandleApiDateTime(DateTime.Now.ToString("dd-MM-yyyy"))
              // pass the current date to the Api handler
              break;
          case:"yesterday":
              HandleApiDateTime(DateTime.Now.AddDays(-1).ToString("dd-MM-yyyy"))
              // pass yesterdays date to the Api handler
              break;
          default:
              Console.Writeline("I didnt recognize this command");
              // or other error handling
              break;
     }
}

private void HandleApiDateTime(DateTime mydate)
{
    // do your api magic with a date object
}
然后,您的功能文件可能看起来像

Given the API returns items for 'yesterday'
when my function is run 
then I want that data in my database

在您的测试中获取当前日期时间的一种最简单的方法是
[StepArgumentTransformation]
,然后您可以从中提取日期或其他内容。通过这种方式,您可以在小黄瓜的其他步骤中使用
[StepArgumentTransformation]
,如下所示,从而减少代码

给定API返回当前时间的项目

给定数据库1返回rightNowTime的项目

给定数据库2返回当前时间的项目

上面只是ex,但基本上它与您喜欢的字符串变量匹配

    public class binding {

            public DateTime datetime;

[Given(@"the API returns items for (.*)")]
[Given(@"Given the database1 returns items for (.*)")] 
[Given(@"Given the database2 returns items for (.*)")] 

 public void currentDatetime(DateTime dt)
            {          
                log.Info("current time: " + datetime);
                 log.Info("current date: " + datetime.Date);

            }

          [StepArgumentTransformation]
           public DateTime convertToDatetime(string c)
            {
                 datetime = DateTime.Now;
                 return datetime;
            }

    }

上面的代码记录了三次当前时间和当前日期

的确,我对真实世界语言的评论是针对Kevin Price给出的示例,他在其功能文件中尝试传递DateTime.now。可以用更好的措辞:-)我对此唯一的抱怨是你选择的措辞(可以更改),但是+1用于指出阶跃变换。它们很方便。
    public class binding {

            public DateTime datetime;

[Given(@"the API returns items for (.*)")]
[Given(@"Given the database1 returns items for (.*)")] 
[Given(@"Given the database2 returns items for (.*)")] 

 public void currentDatetime(DateTime dt)
            {          
                log.Info("current time: " + datetime);
                 log.Info("current date: " + datetime.Date);

            }

          [StepArgumentTransformation]
           public DateTime convertToDatetime(string c)
            {
                 datetime = DateTime.Now;
                 return datetime;
            }

    }