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