Ruby on rails Cucumber:在字段中填入双引号

Ruby on rails Cucumber:在字段中填入双引号,ruby-on-rails,cucumber,Ruby On Rails,Cucumber,我有一些rails应用程序,一个带有字段的视图,比如说它叫做“some_field” 我想填写“带引号的字符串”字段 用黄瓜怎么做 When I fill in "some_field" with ""SOME_STRING"" When I fill in "some_field" with "\"SOME_STRING\"" 不工作(黄瓜解析器似乎不同意) 怎么办?它不是某种匹配器,因此正则表达式也不起作用当您在cucumber场景中编写步骤时,cucumber建议您使用标准正则表达式来

我有一些rails应用程序,一个带有字段的视图,比如说它叫做“some_field” 我想填写“带引号的字符串”字段

用黄瓜怎么做

When I fill in "some_field" with ""SOME_STRING""

When I fill in "some_field" with "\"SOME_STRING\""
不工作(黄瓜解析器似乎不同意)
怎么办?它不是某种匹配器,因此正则表达式也不起作用

当您在cucumber场景中编写步骤时,cucumber建议您使用标准正则表达式来匹配双引号内的文本(正则表达式[^”]*表示任何类型的0个或更多字符的序列,除了“字符”)。因此,对于
,当我用“some\u STRING”填充“some\u field”时,它将建议以下步骤定义:

When /^I fill in "([^"]*)" with: "([^"]*)"$/ do |arg1, arg2|
    pending # express the regexp above with the code you wish you had
end 
@Then("parameter \"(.*?)\" should contain \"(.*?)\"$")
但是您不必被迫使用这个正则表达式,也可以自由编写任何您想要的匹配程序。例如,下面的匹配器将匹配冒号后面并在行尾结束的任何字符串(该字符串可能包含引号):

然后,您的场景步骤将如下所示:

When I fill in "some_field" with: "all " this ' text will ' be matched.
更新 您还可以在场景步骤中使用Cucumber:

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

带引号的字符串将作为最后一个参数传递。在我看来,这种方法看起来比第一种方法更重。

在谷歌搜索时,我发现哪一种(imho)更容易理解(或重新理解)。我正在使用Java,但我相信同样的想法也可以在您的案例中重复使用

不使用此步骤定义:

When /^I fill in "([^"]*)" with: "([^"]*)"$/ do |arg1, arg2|
    pending # express the regexp above with the code you wish you had
end 
@Then("parameter \"(.*?)\" should contain \"(.*?)\"$")
我使用另一个转义字符(从“to/):

因此,我可以使用双引号编写该功能:

Then parameter "getUsers" should contain /"name":"robert","status":"ACTIVE"/

谢谢,我希望有一个更通用的解决方案,而不必为它写一个步骤(例如,我找不到的逃逸角色)如果您的字符串包含双引号,但不包含单引号,那么您可以在场景步骤中将其围成单引号,并适当更改正则表达式。@SirLenz0rlot,我添加了一个替代解决方案非常感谢,我尝试了两种解决方案,并同意您的意见,第一种解决方案会产生更干净的cuccie。您还可以arg1.gsub(“\\”,“)如果在原始功能中转义引号,则引号将被删除。