2010-07-14 30 views
0

我想使用黄瓜来测试我的Rails应用程序的用户的年龄。功能定义看起来有点像以下。自动测试用户的年龄每年都会失败

Scenario: Successful calculation of age 
    Given I set my date of birth to "1987-07-15" 
    Then my age should be "22" 

上面的功能定义每年都会失败,因为用户的年龄会每年增加一个。

一种解决方案可能是将出生日期动态设置为22.years.ago。但我不知道如何在Gherkin(黄瓜特征定义语言)中做到这一点。

对此有何看法?

回答

4

我想补充与此类似

Scenario: Successful calculation of age 
    Given I set my date of birth to "1987-07-15" And the Date is "15/07/2010" 
    Then my age should be "22" 
2

另一个给定的条件,您可以只写

Scenario: Successful calculation of age 
Given I set my date of birth to "22" years ago 
Then my age should be "22" 

,做自己基于第一22日期转换(例如有一个Fixnum参数,以此来测试方法而不是日期)。但说实话我可能会选择伊恩的方法。