【问题标题】:How to use variable in feature file如何在特征文件中使用变量
【发布时间】:2019-09-20 18:30:56
【问题描述】:

如何在功能文件中使用变量?具体来说,我需要使用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 (.*)")]

这是解决此问题的正确方法吗?我不确定如何在我的功能文件中使用变量。我希望我的验收测试使用当前日期

【问题讨论】:

    标签: c# tdd bdd specflow gherkin


    【解决方案1】:

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

    Given the API returns items for right now
    

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

    [Given(@"the API returns items for right now")]
    public void GivenTheAPIReturnsItemsForRightNow()
    {
        GivenTheAPIReturnsItemsFor(DateTime.Now);
    }
    

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

    【讨论】:

      【解决方案2】:

      我部分同意 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
      

      【讨论】:

      • 可以肯定的是,我对现实世界语言的评论指向了 Kevin Price 给出的示例,他试图在他的功能文件中传递 DateTime.now。本来可以说得更好一点:-)
      【解决方案3】:

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

      Given the API returns items for currentTime

      Given the database1 returns items for rightNowTime

      Given the database2 returns items for presentTime

      上面只是 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;
                  }
      
          }
      

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

      【讨论】:

      • 我对此的唯一抱怨是您选择的措辞(可以更改),但 +1 用于指出步骤转换。它们非常方便。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 2021-09-24
      • 2020-07-31
      • 1970-01-01
      相关资源
      最近更新 更多