views:

184

answers:

1

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?

A: 

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
Aurélien Bottazzini
I can see how to use this approach; however, I would have to write special When's for the negative cases. That's OK. I'd been hoping for something like "Except When" so that I would not have special when cases.
RAVolt
In fact, This is what i have done in one of my past project and I am not completely satisfied with it either. But I don't see any other way to do it.If you want something cleaner, maybe you could not raise an error but set an "Error" status code for your action response with an appropriate error message.ex:wants.json { render :json => @user.errors, :status => :unprocessable_entity }
Aurélien Bottazzini