How do I have a negative scenario with Cucumber in

2019-07-24 11:01发布

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条回答
forever°为你锁心
2楼-- · 2019-07-24 11:21

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
查看更多
登录 后发表回答