How to reuse Cucumber step definition with a table for the last parameter?
Asked Answered
P

4

7

This code:

Then %{I should see the following data in the "Feeds" data grid:
                                                   |   Name   |
                                                   | #{name}  |}

And this one:

Then "I should see the following data in the \"Feeds\" data grid:
|   Name   |
| #{name}  |"

And this:

  Then "I should see the following data in the \"Feeds\" data grid:\n|   Name   |\n| #{name}  |"

And even this:

Then <<EOS
I should see the following data in the "Feeds" data grid:
|   Name   |
| #{name}  |
EOS

Gives me:

Your block takes 2 arguments, but the Regexp matched 1 argument.
(Cucumber::ArityMismatchError)
  tests/endtoend/step_definitions/instruments_editor_steps.rb:29:in `/^the editor shows "([^"]*)" in the feeds list$/'
  melomel-0.6.0/lib/melomel/cucumber/data_grid_steps.rb:59:in `/^I should see the following data in the "([^"]*)" data grid:$/'
  tests/endtoend/instruments_editor.feature:11:in `And the editor shows "myFeed" in the feeds list

This one:

Then "I should see the following data in the \"Feeds\" data grid: |   Name   || #{name}  |"

And this one:

Then "I should see the following data in the \"Feeds\" data grid:|   Name   || #{name}  |"

Gives:

Undefined step: "I should see the following data in the "Feeds" data grid:|   Name   || myFeed  |" (Cucumber::Undefined)
  ./tests/endtoend/step_definitions/instruments_editor_steps.rb:31:in `/^the editor shows "([^"]*)" in the feeds list$/'
  tests/endtoend/instruments_editor.feature:11:in `And the editor shows "myFeed" in the feeds list'
Polygyny answered 27/10, 2010 at 12:33 Comment(0)
P
6

I've found the answer myself:

steps %Q{
Then I should see the following data in the "Feeds" data grid:
                                                |   Name   |
                                                | #{name}  |
}
Polygyny answered 27/10, 2010 at 12:59 Comment(0)
W
3

NOTE ON THE ABOVE: might seem obvious, but the new line after the first '{' is soooooo important

Another way:

Given /^My basic step:$/ do |table|
  #do table operation
end


Given /^My referring step:$/ do |table|
  table.hashes.each do |row|    
    row_as_table = %{
      |prop1|prop2|
      |#{row[:prop1]}|#{row[:prop2]}|
    }
    Given %{My basic step:}, Cucumber::Ast::Table.parse(row_as_table, "", 0)
  end
end
Wilsey answered 1/6, 2011 at 1:7 Comment(0)
R
3

You can also write it this way, using #table

Then /^some other step$/ do
  Then %{I should see the following data in the "Feeds" data grid:}, table(%{
    | Name    |
    | #{name} |
  })
end
Rockling answered 1/6, 2011 at 1:29 Comment(0)
G
0

Consider using

Given /^events with:-$/ do |table|
  Given %{I am on the event admin page}
  table.hashes.each do |row|
    Given %{an event with:-}, Cucumber::Ast::Table.new([row]).transpose
  end
end

I find that much more elegant that building up the table by hand.

events with:- gets a table like this

| Form | Element | Label |
| foo  | bar     | baz   |

and an event with:- gets a table like

| Form    | foo |
| Element | bar |
| Label   | baz |
Guildsman answered 26/8, 2011 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.