Sir*_*lot 3 ruby-on-rails cucumber
我有一些rails应用程序,一个带有字段的视图,让我们说它叫'some_field'我想填写字段'"SOME_STRING_WITH_QUOTES"'
我怎么能用黄瓜做这个?
When I fill in "some_field" with ""SOME_STRING""
When I fill in "some_field" with "\"SOME_STRING\""
Run Code Online (Sandbox Code Playgroud)
不工作(黄瓜解析器似乎不赞成)怎么办?它不是某种匹配器,所以正则表达式也不会工作
当您在黄瓜场景中编写一个步骤时,Cucumber建议您使用标准正则表达式匹配双引号内的文本(正则表达式[^"]*表示任何类型的0或更多字符的序列,"字符除外".因此,When I fill in "some_field" with: "SOME_STRING"它将建议以下步骤定义:
When /^I fill in "([^"]*)" with: "([^"]*)"$/ do |arg1, arg2|
pending # express the regexp above with the code you wish you had
end
Run Code Online (Sandbox Code Playgroud)
但是你没有被迫使用这个正则表达式并且可以自由地编写你想要的任何匹配器.例如,以下匹配器将匹配冒号后面的任何字符串,并在行结束处结束(此字符串可能包含引号):
When /^I fill in "([^"]*)" with: (.*)$/ do |field, value|
pending # express the regexp above with the code you wish you had
end
Run Code Online (Sandbox Code Playgroud)
然后您的方案步骤将如下所示:
When I fill in "some_field" with: "all " this ' text will ' be matched.
Run Code Online (Sandbox Code Playgroud)
您还可以在场景步骤中使用Cucumber的多行字符串:
When I fill in "some_field" with:
"""
your text " with double quotes
"""
Run Code Online (Sandbox Code Playgroud)
然后你不需要改变Cucumber生成的步骤定义:
When /^I fill in "([^"]*)" with:$/ do |arg1, string|
pending # express the regexp above with the code you wish you had
end
Run Code Online (Sandbox Code Playgroud)
带引号的字符串将作为最后一个参数传递.在我看来,这种方法看起来比第一种更重.
| 归档时间: |
|
| 查看次数: |
4060 次 |
| 最近记录: |