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

より動的なものに (行ごとに個別のステップを作成する代わりに)

ここに私のWebステップのサンプルがあります

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を使用し、その上で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"

そして、「最初」、「2番目」などを取得するためのアクションビューヘルパーnumber_in_wordsがあります

申し訳ありませんが、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 に答える