-1

I'm working on the refactor of my RSpec+ capybara-webkit tests, and I I'm trying to not have lot of repetitions.
So I created this method that I will call various times during the tests:

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

And this is my test:

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

I'd like to have something very simple like

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

but I cannot achieve it so easily.

I tried also with

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

but RSpec will search for a field called "field1" and not for "first_field" (that I'm passing to the method).

fabdurso
  • 2,366
  • 5
  • 29
  • 55

3 Answers3

3

You should pass a hash of fields and values:

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
sawa
  • 165,429
  • 45
  • 277
  • 381
1

Here's a solution that will work for any n pairs of fields and values:

def fill(fields, values, button)
    (fields.zip values).each { |field, value| fill_in field, :with => value }
    find(button).click
end

The call is a little different:

fill(['first_field', 'second_field', 'thrid_field'], ['first_value', 'second_value', 'third_value'], 'input.button')
Leo Brito
  • 2,053
  • 13
  • 20
  • I see not reason for using `Hash[]` and `each`. You can directly pass the block to `zip`. – sawa Dec 17 '15 at 17:41
  • Thanks @sawa, I updated the answer as to not use Hash. I don't understand what you mean by "there is no reason to use `each`", could you detail a little more on what you're thinking? – Leo Brito Dec 17 '15 at 17:52
-1
(1..3).each do |i|
  fill_in eval("field#{i}"), with: eval("value#{1}")
end
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • As i said in the question, i tried something like this but it will look for a filed called "field1" that it's not what i'm looking for – fabdurso Dec 17 '15 at 17:32
  • @fabersky This answer is different from what you tried. The evaluated value of `eval("field#{i}")` is `"first_field"` and so on. – sawa Dec 17 '15 at 17:35
  • 1
    @sawa I do not care about downvotes; appreciate the upvote though. Being afraid of `eval` in tests is just insane. – Aleksei Matiushkin Dec 17 '15 at 18:25
  • Needlessly using `eval` to solve this problem is equally insane. – user229044 Dec 18 '15 at 15:56
  • @meagar Assuming you are the moderator, you probably able to read the question title, which apparently is “Variable name with another variable value on it to achieve less repetitions.” The upvoted and selected answers both suggest the different approach, mine resolves the problem that was stated in the title. Whether you might think that answering questions as they stated is “needless insanity,” I keep my own opinion on the topic. – Aleksei Matiushkin Dec 18 '15 at 16:28