Rails 中的 Cucumber 如何出现负面情况?

发布于 2024-08-06 20:44:11 字数 511 浏览 1 评论 0原文

我有一个负面场景需要用 Cucumber 进行测试。具体来说,我想确保当有人发布带有无效句柄的 URL 时,网站会返回错误。

我的场景如下:

场景:创建手柄太短的人 当名为“Fred”且句柄“太短”的人更新时 那么我应该得到 500 错误

我的步骤看起来像

当/^名为“(.)”且句柄为“(.)”的人更新$/时,执行|名称,句柄| 访问“/mobile/update?handle=#{udid}&name=#{name}”

当我运行场景时,由于 When 的错误,它永远不会到达 THEN 部分

错误:无句柄(运行时错误)

这是正确的,此时应出现 500 错误。

我只是不知道如何将“何时”表述为阴性测试。也许我应该使用与when不同的东西?

I have a negative scenario to test with Cucumber. Specifically, I want to make sure that when someone posts a URL with an invalid handle then the site returns an error.

My scenario looks like:

Scenario: create person with too short a handle
When person named "Fred" with handle "tooshort" updates
Then I should get a 500 error

My step looks like

When /^person named "(.)" with handle "(.)" updates$/ do |name, handle|
visit "/mobile/update?handle=#{udid}&name=#{name}"

When I run the scenario, it never gets to the THEN part because of the error from the When

ERROR: No Handle (RuntimeError)

This is CORRECT, the When should turn a 500 error.

I just don't know how to phrase the When as a negative test. Maybe I should use something different than a when?

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

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

发布评论

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

评论(1

迷路的信 2024-08-13 20:44:11

如果您的 When 步骤的正确行为是引发错误,您可以使用 lambda 块来捕获此 RuntimeError:

When /^person named "(.)" with handle "(.)" updates$/ do |name, handle| 
  lambda { 
    visit "/mobile/update?handle=#{udid}&name=#{name}"
  }.should raise_error("No Handle")
end

您可能需要调整 raise_error 部分,因为我不确切知道您引发的错误类型

并且您可以有一个像这样的“然后”步骤(起点)

Then /^the save should not be successful$/ do
  response.should be_nil
end

If for you the correct behavior of your When step is to raise an error you case use a lambda block to catch this RuntimeError:

When /^person named "(.)" with handle "(.)" updates$/ do |name, handle| 
  lambda { 
    visit "/mobile/update?handle=#{udid}&name=#{name}"
  }.should raise_error("No Handle")
end

You may have to tweak the raise_error part as I don't know exactly the type of error you are raising

And you can have a Then step like this one (starting point)

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