2013-05-09 120 views
0

我有這樣的場景:黃瓜步驟:我如何從不同的步驟獲取輸入數據?

Given I am on the edit_stamp page 
And I change the date to "14.5.2010"    #<-- i need this data 
... 
Then I should see that the new times has been set #<-- down here 

我基本上更新模型的日期,並在最後一步,我想驗證模型確實與我在第一步選擇的日期更新。

我怎樣才能從最上面的步驟定義中抓取選定的日期?

Then(/^I should see that the new times has been set$/) do 
    s = Stamp.first 
    find_by_id("day_date#{s.id}").has_text?("14.5.2010") 
end 

這是我現在有,但我不想寫日期(2010年5月14日)進入步驟清晰,我想從前面的步驟獲取它。

回答

2

試試這個:

And (/^I change the date to "(.*?)"$/) do |input_date| 
    @new_date = input_date 
    # and here do whatever you are doing with the date 
end 

Then(/^I should see that the new times has been set$/) do 
    s = Stamp.first 
    s.date_or_whatever_attribute_you_are_using.to_s.should == @new_date 
end 

這些實例變量(@xx)堅持沿着整個場景。

+1

順便說一句,在你的步驟中使用實例變量被認爲是不好的做法。它使得它的步驟不依賴於它們的運行順序。更多tid位[這裏](http://www.slideshare.net/AtreyeeMaiti/keeping-your-cucumber-suite-maintainable-11140647)。 – MrDanA 2013-05-09 13:13:17