变量名加上另一个变量值以减少重复
Variable name with another variable value on it to achieve less repetitions
我正在重构我的 RSpec
+ capybara-webkit
测试,我
我尽量不重复很多。
所以我创建了这个方法,我将在测试期间多次调用它:
def fill(field1, value1, field2, value2, field3, value3, button)
fill_in field1, :with => value1
fill_in field2, :with => value2
fill_in field3, :with => value3
find(button).click
end
这是我的测试:
describe "Test", :js => true do
it "fill fields" do
fill('first_field', 'first_value', 'second_field', 'second_value, 'third_field', 'third_value', 'input.button')
end
end
我想要一些非常简单的东西,比如
def fill(field1, value1, field2, value2, field3, value3, button)
for i in 1..3 do
fill_in field+"#{i}", :with => value+"#{i}"
end
find(button).click
end
但我没那么容易做到。
我也试过
def fill(field1, value1, field2, value2, field3, value3, button)
for i in 1..3 do
fill_in "field#{i}", :with => "value#{i}"
end
find(button).click
end
但 RSpec
将搜索名为 "field1" 的字段而不是 "first_field"(我正在传递给该方法)。
(1..3).each do |i|
fill_in eval("field#{i}"), with: eval("value#{1}")
end
这是一个适用于任何 n 对字段和值的解决方案:
def fill(fields, values, button)
(fields.zip values).each { |field, value| fill_in field, :with => value }
find(button).click
end
调用有点不同:
fill(['first_field', 'second_field', 'thrid_field'], ['first_value', 'second_value', 'third_value'], 'input.button')
您应该传递字段和值的哈希值:
def fill(button, h)
h.each{|field, value| fill_in field, with: value}
find(button).click
end
describe "Test", js: true do
it "fill fields" do
fill('input.button', 'first_field' => 'first_value', 'second_field' => 'second_value', 'third_field' => 'third_value')
end
end
我正在重构我的 RSpec
+ capybara-webkit
测试,我
我尽量不重复很多。
所以我创建了这个方法,我将在测试期间多次调用它:
def fill(field1, value1, field2, value2, field3, value3, button)
fill_in field1, :with => value1
fill_in field2, :with => value2
fill_in field3, :with => value3
find(button).click
end
这是我的测试:
describe "Test", :js => true do
it "fill fields" do
fill('first_field', 'first_value', 'second_field', 'second_value, 'third_field', 'third_value', 'input.button')
end
end
我想要一些非常简单的东西,比如
def fill(field1, value1, field2, value2, field3, value3, button)
for i in 1..3 do
fill_in field+"#{i}", :with => value+"#{i}"
end
find(button).click
end
但我没那么容易做到。
我也试过
def fill(field1, value1, field2, value2, field3, value3, button)
for i in 1..3 do
fill_in "field#{i}", :with => "value#{i}"
end
find(button).click
end
但 RSpec
将搜索名为 "field1" 的字段而不是 "first_field"(我正在传递给该方法)。
(1..3).each do |i|
fill_in eval("field#{i}"), with: eval("value#{1}")
end
这是一个适用于任何 n 对字段和值的解决方案:
def fill(fields, values, button)
(fields.zip values).each { |field, value| fill_in field, :with => value }
find(button).click
end
调用有点不同:
fill(['first_field', 'second_field', 'thrid_field'], ['first_value', 'second_value', 'third_value'], 'input.button')
您应该传递字段和值的哈希值:
def fill(button, h)
h.each{|field, value| fill_in field, with: value}
find(button).click
end
describe "Test", js: true do
it "fill fields" do
fill('input.button', 'first_field' => 'first_value', 'second_field' => 'second_value', 'third_field' => 'third_value')
end
end