How do I have a negative scenario with Cucumber in Rails?

戏子无情 提交于 2019-12-24 10:55:39

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/1548714/how-do-i-have-a-negative-scenario-with-cucumber-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!