问题
I am a newbie to Cucumber-Ruby. After defining the scenario, I executed the test in the terminal and cucumber suggested the snippet as follows :
Then("Show All button should be enabled") do
pending # Write code here that turns the phrase above into concrete actions
end
Then("Show All button should be disabled") do
pending # Write code here that turns the phrase above into concrete actions
end
I changed the code as below
Then("Show All button should be (enabled|disabled)") do |state|
puts(state)
end
But even after that when I execute the test using the terminal I receives the suggestion to add the snippet.
When I changed the code as below it worked
Then(/^Show All button should be (enabled|disabled)$/) do |state|
puts(state)
end
Can someone help me describing the difference between the codes?
USING
Ruby : ruby 2.3.3p222 Cucumber : 3.1.0
回答1:
Cucumber compares the argument passed to Then against scenario titles using case-equal aka triple-equal. That said, for the scenario title "Foo Bar" and the snippet Then something do, it executes
something === "Foo Bar"
For strings, triple-equal is aliased to == and since "Show ... (enabled|disabled)" string is not equal to "Show ... enabled" nor to "Show ... disabled", nothing is matched.
OTOH, when you change the argument to Regexp, it matches:
/^Show ... (enabled|disabled)$/ === "Show ... enabled"
#⇒ true
That is why the latter snippet effectively works.
来源:https://stackoverflow.com/questions/50850727/state-variable-cucumber-ruby-regular-expressions