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
-
1\$\begingroup\$ Is this actual code? It reads very much like pseudo-code.. \$\endgroup\$Mathieu Guindon– Mathieu Guindon2016年02月26日 21:55:41 +00:00Commented Feb 26, 2016 at 21:55
-
1\$\begingroup\$ Yeah, Cucumber generates that reaction a lot, but this code actually runs. \$\endgroup\$Dan Kohn– Dan Kohn2016年02月26日 22:03:20 +00:00Commented 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– Jamal2016年02月26日 22:04:28 +00:00Commented 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– Dan Kohn2016年02月27日 14:06:18 +00:00Commented 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– Malachi2016年02月27日 15:12:01 +00:00Commented Feb 27, 2016 at 15:12
1 Answer 1
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.
-
\$\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– Dan Kohn2016年04月04日 14:40:20 +00:00Commented Apr 4, 2016 at 14:40
-
\$\begingroup\$ I have updated my answer \$\endgroup\$kfairns– kfairns2016年04月04日 15:19:48 +00:00Commented 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– Dan Kohn2016年04月04日 20:38:28 +00:00Commented Apr 4, 2016 at 20:38