红宝石或铁轨中有一个主要功能吗?

时间:2011-05-06 05:20:24

标签: ruby-on-rails ruby cucumber cardinality ordinals

我正试图找到一种更好的方式来表达我的黄瓜,所以我正在寻找一个序数到基数函数来转换它:

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

3 个答案:

答案 0 :(得分:19)

我真的不完全理解,你究竟想做什么,但你可以通过积极的支持做这样的事情:

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

并且有一个动作视图助手number_in_words来获得“第一”,“第二”等

我对cukes抱歉不太了解,

答案 1 :(得分:7)

使用简短形式,易于解析的序数:

When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
  # etc...
end

答案 2 :(得分:4)

此功能内置于Chronic

irb(main):001:0> require 'chronic'
=> true
irb(main):002:0> Chronic::Numerizer.numerize("eighty-fifth").to_i
=> 85