State variable-Cucumber - Ruby -Regular expressions

血红的双手。 提交于 2019-12-13 03:53:03

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!