当您在 Cucumber 场景中编写步骤时,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
但您不必被迫使用此正则表达式,可以自由地编写您想要的任何匹配器。例如,以下匹配器将匹配冒号之后并在行尾结束的任何字符串(并且该字符串可能包含引号):
When /^I fill in "([^"]*)" with: (.*)$/ do |field, value|
pending # express the regexp above with the code you wish you had
end
那么您的场景步骤将如下所示:
When I fill in "some_field" with: "all " this ' text will ' be matched.
Update
您也可以使用黄瓜多行字符串 https://github.com/cucumber/cucumber/wiki/Multiline-Step-Arguments在您的场景步骤中:
When I fill in "some_field" with:
"""
your text " with double quotes
"""
然后你就不需要更改 Cucumber 生成的步骤定义:
When /^I fill in "([^"]*)" with:$/ do |arg1, string|
pending # express the regexp above with the code you wish you had
end
带引号的字符串将作为最后一个参数传递。在我看来,这种方法看起来比第一种方法更重。