如何在特征文件中使用变量

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

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


ibeautiful
浏览 52回答 2
2回答

吃鸡游戏

最简单的方法是编写一个特定于“立即”返回行项目的步骤:Given the API returns items for right now您可以从新版本调用该步骤的其他版本:[Given(@"the API returns items for right now")]public void GivenTheAPIReturnsItemsForRightNow(){    GivenTheAPIReturnsItemsFor(DateTime.Now);}这避免了步骤之间的代码重复。

杨魅力

由于功能文件是您可以与业务利益相关者(或组织中的其他非 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
打开App,查看更多内容
随时随地看视频慕课网APP