如何在Rails中使用Cucumber有一个负面情景? [英] How do I have a negative scenario with Cucumber in Rails?

查看:268
本文介绍了如何在Rails中使用Cucumber有一个负面情景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个负面情景来测试用黄瓜。具体来说,我想确保当有人发布一个包含无效句柄的网址时,网站会返回一个错误。



我的场景如下:


场景:创建一个句柄太短的人
当用tooshort命名为Fred的人更新
然后我应该得到一个500错误


我的步骤看起来像


当/ ^ person命名为(。)时使用句柄(。)更新$ / do | name,handle |
visit/ mobile / update?handle =#{udid}& name =#{name}


我运行的方案,它从来没有得到到THEN部分,因为错误从


错误:无句柄(RuntimeError) p>

这是CORRECT,When应该会出现500错误。



不知道怎么用短语作为负面测试。也许我应该使用不同于什么时候?

解决方案

如果你的正确行为,当你的步骤是提出一个错误你case使用lambda块来捕获这个RuntimeError :

 当/ ^ person以(。)命名为。 | 
lambda {
visit/ mobile / update?handle =#{udid}& name =#{name}
} .should raise_error(No Handle)
end

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

 

code>然后/ ^保存不应该成功$ / do
response.should be_nil
end


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?

解决方案

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

这篇关于如何在Rails中使用Cucumber有一个负面情景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆