10

我正在尝试找到一种更好的方法来表达我的黄瓜,所以我正在寻找一个将其转换为基数的序数函数:

When I fill up the first passenger field
Then I should see the passenger list update with the first passenger details
When I follow "Add Another Passenger"
Then I should see a second passenger field
When I fill up the second passenger field
Then I should see the passenger list update with the second passenger details

变成更动态的东西(而不是为每一行创建单独的步骤)

这是我的网络步骤示例

When /^I fill up the first passenger field$/ do
  fill_in("booking_passengers_attributes_0_first_name", :with => "Blah")
  fill_in("booking_passengers_attributes_0_last_name", :with => "blah")
  select("5' to 6'", :from => "booking_passengers_attributes_0_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_0_weight")
end

When /^I fill up the second passenger field$/ do
  fill_in("booking_passengers_attributes_1_first_name", :with => "Wee")
  fill_in("booking_passengers_attributes_1_last_name", :with => "Sir")
  select("5' to 6'", :from => "booking_passengers_attributes_1_height")
  select("150 to 200lbs", :from => "booking_passengers_attributes_1_weight")
end

看到 0 和 1 了吗?我希望将“第一”转换为基数,这样我就可以替换了。您也可以建议一种更好的方式来声明杯子:)

更新的答案 我正在重构,但基本上我使用 1st 而不是 first 并使用 to_i 。

When /^I fill up the "([^"]*)" passenger field$/ do |id|
  input_id = id.to_i - 1
  fill_in("booking_passengers_attributes_#{input_id}_first_name", :with => id)
  fill_in("booking_passengers_attributes_#{input_id}_last_name", :with => "Passenger")
  select("5' to 6'", :from => "booking_passengers_attributes_#{input_id}_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_#{input_id}_weight")  
end
4

3 回答 3

19

我真的不完全明白,究竟你想做什么,但你可以在积极的支持下做这样的事情:

 1.ordinalize    # => "1st"
  2.ordinalize    # => "2nd"
  1002.ordinalize # => "1002nd"

并且有一个动作视图助手 number_in_words 来获取 "first" 、 "second" 等

我对cukes了解不多,抱歉,

于 2011-05-06T05:29:06.217 回答
7

使用简短的、易于解析的序数:

When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
  # etc...
end
于 2011-05-06T05:41:18.220 回答
4

此功能内置于Chronic中:

irb(main):001:0> require 'chronic'
=> true
irb(main):002:0> Chronic::Numerizer.numerize("eighty-fifth").to_i
=> 85
于 2012-11-02T20:04:46.807 回答