如何使specflow能够很好地应对日期/时间?

Ian*_*ose 5 .net specflow gherkin

我希望能够编写这样的测试:

Background:
  Given a user signs up for a 30 day account

Scenario: access before expiry
  When they login in 29 days
  Then they will be let in

Scenario: access after expiry
  When they login in 31 days
  Then they will be asked to renew

Scenario: access after acounnt deleted
  When they login in 2 years time
  Then they will be asked to register for a new account
Run Code Online (Sandbox Code Playgroud)

如何进行测试的specflow方面?

编辑:相同的步骤定义如何处理"31天"和"2年时间"

Sub*_*ity 2

我想您可能正在寻找StepArgumentTransformation

为了应对“31 天内”的情况,文档为您准备了:

[Binding]
public class Transforms
{
    [StepArgumentTransformation(@"in (\d+) days?")]
    public DateTime InXDaysTransform(int days)
   {
      return DateTime.Today.AddDays(days);
   }
}
Run Code Online (Sandbox Code Playgroud)

对于“两年内”,你可以看到这种模式......

    [StepArgumentTransformation(@"in (\d+) years?")]
    public DateTime InXYearsTransform(int years)
   {
      return DateTime.Today.AddYears(years);
   }
Run Code Online (Sandbox Code Playgroud)