我的正则表达式在 Cucumber 中无法识别 - 救命!

发布于 2024-10-21 19:10:39 字数 619 浏览 2 评论 0原文

所以这是一个非常新手的问题。

我有一个如下所示的步骤:

Then /^I should see ((phrase 1) | (phrase 2))$/ do
  #stuff
end

之后,我有一个如下所示的步骤:

Then /^I should see phrase 3$/ do
  And %{I should see phrase 2}
end

我正在尝试测试注册流程。

phrase 1 描述了需要填写的必填信息,我需要验证它们是否显示在用户的个人资料中。

phrase 3 描述了用户填写的必填信息和可选信息。可选信息也应该显示在用户的个人资料中,我想测试它是否也会显示。

我不想复制并粘贴第一步中的内容,因此我重新措辞了phrase 1,以便从phrase 3 步骤调用是有意义的,但我不知道如何在第一步中正确引用它以使其发挥作用。我从 Cucumber 得到的只是我应该看到短语 2 是未定义的。

完成这项工作的正确方法是什么?有更好的方法来实现我的目标吗?

So this is a really newby question.

I have a step that looks like this:

Then /^I should see ((phrase 1) | (phrase 2))$/ do
  #stuff
end

After that, I have a step that looks like this:

Then /^I should see phrase 3$/ do
  And %{I should see phrase 2}
end

I'm trying to test a registration flow.

phrase 1 describes the required information that needs to be filled out and I need to verify that they show up the in the user's profile.

phrase 3 describes the required and optional information the user fills out. The optional information should aslo show up in the user's profile and I wanna test that that also shows up.

I don't want to copy and paste the stuff from the first step, so I re-phrased phrase 1 so that it makes sense to be called from the phrase 3 step, but I don't know how to properly refer to it in the first step to get it to work. All I get from Cucumber is that I should see phrase 2 is undefined.

What's the right way to get this work? Is there a better way to accomplish my goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何止钟意 2024-10-28 19:10:39

空格字符在正则表达式中非常重要

如果管道两端有空格,您需要使用此步骤:

Then %{I should see phrase 1 }

Then %{I should see  phrase 2}

(请注意第一个末尾处以及 see之间的额外空格第二个短语2)。正则表达式仍在查找您指定的空格字符。

您的正则表达式需要更改为:

Then /^I should see (phrase 1|phrase 2)$/ do |phrase_name|

end

正则表达式中的 | 符号将匹配左侧的所有字符,左侧的所有字符右侧(括号当然会限制它,因此它不会占用整个字符串)。

此外,您还必须将值传递给您的块,就像我在以这种方式使用括号时对 phrase_name 所做的那样。尽管我可能更愿意看到这样定义的步骤:

Then /^I should see phrase ([\d]+)$/ do |phrase_number|

end

这样它将匹配您的所有三种短语类型,并且您不再需要短语 1 定义。 [\d]+ 将匹配任何数字字符 1 次或多次。

然后您可以根据发送的phrase_number 执行您需要执行的操作。我很想看看你的场景定义,但我不确定你是否会根据这些信息正确地进行处理。如果我看到故事的其余部分,也许我可以了解一些背景信息。

Whitespace characters are very important in regex's

With whitespace at either end of the pipe you need to use this step:

Then %{I should see phrase 1 }

or

Then %{I should see  phrase 2}

(Note that extra space at the end on the first one, and between see and phrase2 on the second one). The regex is still looking for those space characters where you specified them to be.

Your regex would need to be changed to:

Then /^I should see (phrase 1|phrase 2)$/ do |phrase_name|

end

The | symbol in regex will match either all characters on the left side, or all characters on the right side (the parantheses of course bounds it so it doesn't take the entire string).

Also you have to pass the value to your block, as I did with phrase_name when you use parenthesis in this manner. Although I would probably rather see the step defined this way:

Then /^I should see phrase ([\d]+)$/ do |phrase_number|

end

So that it will match for all three of your phrase types, and you wouldn't need the phrase 1 definition anymore. [\d]+ will match any numerical character 1 or more times.

Then you can do whatever you need to do based on the phrase_number that was sent. I'd be interested to see your scenario definition though, I'm not sure if you're going about cuking it correctly based on this information. Maybe I could get some context if I saw the rest of the story.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文