Skip to content

Commit f8dbb56

Browse files
MadameSheemabrasmusson
authored andcommitted
Adds scenario for the ambiguous step reporting (#1132)
* Adds scenario for the ambiguous step reporting * Adds @wip tag * Adds feature documentation * Simplifies tests scenarios by removing the scenario name * Adds ambiguous tests with guess mode scenario * Simplifies ambiguous steps scenario * Improves test scenarios by adding keywords * Adds summary report to ambiguous steps scenario * Adds @wip tag to ambiguous steps scenario * Add ambiguous step matching Taught Cucumber how to match against ambiguous steps. * Fix the feature and implementation of ambiguous steps.
1 parent 6ccedba commit f8dbb56

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
Feature: Ambiguous Steps
2+
3+
When Cucumber searches for a step definition for a step, it might find multiple step
4+
definitions that could match. In that case, it will give you an error that the step
5+
definitions are ambiguous.
6+
7+
You can also use a `--guess` mode, where it uses magic powers to try and figure
8+
out which of those two step definitions is most likely to be the one you meant it
9+
to use. Use it with caution!
10+
11+
12+
Scenario: Ambiguous steps
13+
14+
Given a file named "features/ambiguous.feature" with:
15+
"""
16+
Feature:
17+
18+
Scenario:
19+
When a step
20+
Then an ambiguous step
21+
22+
"""
23+
And a file named "features/step_definitions.rb" with:
24+
"""
25+
When(/^a.*step$/) do
26+
'foo'
27+
end
28+
29+
Then(/^an ambiguous step$/) do
30+
'bar'
31+
end
32+
33+
"""
34+
When I run `cucumber`
35+
Then it should fail with:
36+
"""
37+
Ambiguous match of "an ambiguous step":
38+
39+
features/step_definitions.rb:1:in `/^a.*step$/'
40+
features/step_definitions.rb:5:in `/^an ambiguous step$/'
41+
42+
You can run again with --guess to make Cucumber be more smart about it
43+
(Cucumber::Ambiguous)
44+
features/ambiguous.feature:5:in `Then an ambiguous step'
45+
46+
Failing Scenarios:
47+
cucumber features/ambiguous.feature:3 # Scenario:
48+
49+
1 scenario (1 failed)
50+
2 steps (1 failed, 1 passed)
51+
0m0.012s
52+
53+
"""
54+
55+
56+
Scenario: Ambiguous steps with guess mode
57+
58+
Given a file named "features/ambiguous.feature" with:
59+
"""
60+
Feature:
61+
62+
Scenario:
63+
When a step
64+
Then an ambiguous step
65+
"""
66+
And a file named "features/step_definitions.rb" with:
67+
"""
68+
When(/^a.*step$/) do
69+
'foo'
70+
end
71+
72+
Then(/^an ambiguous step$/) do
73+
'bar'
74+
end
75+
"""
76+
When I run `cucumber -g`
77+
Then it should pass with exactly:
78+
"""
79+
Feature:
80+
81+
Scenario: # features/ambiguous.feature:3
82+
When a step # features/step_definitions.rb:1
83+
Then an ambiguous step # features/step_definitions.rb:5
84+
85+
1 scenario (1 passed)
86+
2 steps (2 passed)
87+
0m0.012s
88+
89+
"""

lib/cucumber/filters/activate_steps.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ def initialize(step_match_search, configuration, test_step)
4343
end
4444

4545
def result
46-
return NoStepMatch.new(test_step.source.last, test_step.name) unless matches.any?
46+
begin
47+
return NoStepMatch.new(test_step.source.last, test_step.name) unless matches.any?
48+
rescue Cucumber::Ambiguous => e
49+
return AmbiguousStepMatch.new(e)
50+
end
51+
4752
configuration.notify :step_activated, test_step, match
4853
return SkippingStepMatch.new if configuration.dry_run?
4954
match

lib/cucumber/step_match.rb

+13
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,17 @@ def activate(test_step)
140140
return test_step
141141
end
142142
end
143+
144+
class AmbiguousStepMatch
145+
146+
def initialize(error)
147+
@error = error
148+
end
149+
150+
def activate(test_step)
151+
return test_step.with_action { raise @error }
152+
end
153+
154+
end
155+
143156
end

0 commit comments

Comments
 (0)