1
\$\begingroup\$

Cucumber is not designed to support multi-column iterations, but it is possible to make it work. Here, I want to try each combination of path and role.

This is the Cucumber Gherkin:

  Scenario: cannot access paths
    When I access "path" as "role" then I should see an error
      | /path1 | user1 |
      | /path2 | user2 |
      | /path3 | user3 |
      | /path4 |       |

And this is the Cucumber step_definition:

When(/^I access "path" as "role" then I should see an error$/) do |table|
  paths, roles = table.raw.transpose.map { |e| e.reject(&:blank?) }
  roles.each do |role|
    step "I am logged in as #{role}"
    paths.each do |path|
      p "#{role} user visiting #{path}"
      visit path
      step 'I should see the privileges error'
    end
  end
end

table.raw looks like:

[
    [0] [
        [0] "/path1",
        [1] "user1"
    ],
    [1] [
        [0] "/path2",
        [1] "user2"
    ],
    [2] [
        [0] "/path3",
        [1] "user3"
    ],
    [3] [
        [0] "/path4",
        [1] ""
    ]
]

and table.raw.transpose produces:

[
    [0] [
        [0] "/path1",
        [1] "/path2",
        [2] "/path3",
        [3] "/path4"
    ],
    [1] [
        [0] "user1",
        [1] "user2",
        [2] "user3",
        [3] ""
    ]
]

I like the line with transpose, but is it possible to make it any more succinct?

See Reference Cucumber for documentation related to Cucumber

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Is this actual code? It reads very much like pseudo-code.. \$\endgroup\$ Commented Feb 26, 2016 at 21:55
  • 1
    \$\begingroup\$ Yeah, Cucumber generates that reaction a lot, but this code actually runs. \$\endgroup\$
    – Dan Kohn
    Commented Feb 26, 2016 at 22:03
  • 1
    \$\begingroup\$ Which part at the top is the actual code? Is "When" a part of it? \$\endgroup\$
    – Jamal
    Commented Feb 26, 2016 at 22:04
  • \$\begingroup\$ I edited to separate the Gherkin from the step definition. The Ruby code I'm asking about is the step definition. \$\endgroup\$
    – Dan Kohn
    Commented Feb 27, 2016 at 14:06
  • \$\begingroup\$ you might want to add a web reference to Cucumber to the question here, so that people have something to refer to when reviewing your code. there are reviewers here that will read the documentation just so they can review code. \$\endgroup\$
    – Malachi
    Commented Feb 27, 2016 at 15:12

1 Answer 1

1
\$\begingroup\$

For this you could use a Scenario Outline, and I personally, would split that function into two separate functions.

Try doing something like this:

Scenario Outline: cannot access certain paths as roles
  When I access "<path>" as "<role>" 
  Then I should see an error

Examples:
 | path   | role  |
 | /path1 | user1 |
 | /path2 | user2 |
 | /path3 | user3 |
 | /path4 |       |

Scenario Outlines are used when you want to test the same fundamental thing, using different parameters, without clogging up your feature file with many Scenarios.

They always use an examples table, and will run through the examples table replacing the placeholders (which will always match the heading rows), with the content of each row (as with a usual table).

In my opinion, this is the best way to test what you are attempting to do.

EDIT

In light of the comments below, maybe this would be the best course of action:

Scenario Outline: cannot access various paths as role 1
  When I access "<path>" as "user1" 
  Then I should see an error

Examples:
| path   |
| /path1 |
| /path2 |
| /path3 |
| /path4 |

Scenario Outline: cannot access various paths as role 2
  When I access "<path>" as "user2" 
  Then I should see an error

Examples:
| path   |
| /path1 |
| /path2 |
| /path3 |
| /path4 |

Scenario Outline: cannot access various paths as role 3
  When I access "<path>" as "user3" 
  Then I should see an error

Examples:
| path   |
| /path1 |
| /path2 |
| /path3 |
| /path4 |

It may be longer, but it is more human readable than perhaps the table would.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, that's the standard Cucumber way, but won't actually accomplish my goals, which is to run 4 x 3 = 12 examples. Yours will just run 4. \$\endgroup\$
    – Dan Kohn
    Commented Apr 4, 2016 at 14:40
  • \$\begingroup\$ I have updated my answer \$\endgroup\$ Commented Apr 4, 2016 at 15:19
  • \$\begingroup\$ Thanks. Your updated answer is the "official" way to do it. I was just looking for a less wordy shortcut. \$\endgroup\$
    – Dan Kohn
    Commented Apr 4, 2016 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.