状态变量-黄瓜-Ruby-正则表达式 [英] State variable-Cucumber - Ruby -Regular expressions

查看:163
本文介绍了状态变量-黄瓜-Ruby-正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cucumber-Ruby的新手。定义场景之后,我在终端中执行了测试,黄瓜提示了如下代码段:



然后(应该启用显示所有按钮)执行



待处理#在此处编写代码,使上面的短语变为进入具体动作



结束



然后( 应该禁用显示所有按钮)执行



pending#在此处编写代码,将上面的短语变为具体动作



end



I更改了以下代码



然后((显示所有按钮应为(已启用|已禁用))执行 |状态|



输入(状态)



结束



但是即使在那之后,当我使用终端执行测试时,我仍然收到添加代码段的建议。



当我更改下面的代码时,它起作用了



然后(//显示全部按钮应为(启用|禁用)$ /)执行 |状态|



输入(状态)



结束



有人可以帮助我描述代码之间的区别吗?



使用



Ruby:红宝石2.3.3p222
黄瓜:3.1.0

解决方案

Cucumber使用大小写相等又称为三重相等来比较传递给 Then 的自变量与方案标题。也就是说,对于场景标题 Foo Bar 和摘要然后执行某操作,它将执行

 某物=== Foo Bar 

对于字符串,三等号别名为 == ,因为 Show ...(启用|禁用) 字符串不等于显示...已启用 也不等于显示...已禁用 ,没有任何匹配项。



OTOH,当您将参数更改为 Regexp 时,它匹配项

  / ^ Show ...(enabled | disabled)$ / === Show。 .. enabled 
#⇒真正的

这就是后面的代码段有效工作的原因。 / p>

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

解决方案

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.

这篇关于状态变量-黄瓜-Ruby-正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆