In case of failure the normal should be_redirect and its friends are far from helpful…
expected redirect? to return true, got false expected "new", got nil
Now you can have this:
Status should be redirect but was 200(success) - rendered addresses/new - Flash: :error = Address contains errors! - Errors:Errors on @address(Address): City can't be blank
Usage
- response.should redirect_to / render_template as normal
- response.should have_been_success (i do not want to overwrite be_success…)
- have_been_error
- have_been_missing
- have_been_redirect
Install
script/plugin install git://github.com/grosser/rspec_response_enhancer.git add to spec/spec_helper.rb: Spec::Runner.configure do |config| ... config.include(RspecResponseEnhancer) ... end
And as added benefit you no longer need to build should be_success when you already have a should render_template, since render_template now gives detailed error messages!
Does this work with the latest rspec?
I’m getting this:
2)
NoMethodError in ‘Admin::xyzcontroller responding to POST create with invalid params should re-render the ‘new’ template’
undefined method `has_been_success?’ for #
./spec/controllers/admin_xyz_spec.rb:98:
thanks for the tip, the install instructions where missing here…
i refactored the code so that have_been_success/have_succeeded (without the ?) should work out of the box
please update and
add to spec/spec_helper.rb:
Spec::Runner.configure do |config|
…
config.include(RSpecResponseEnhancer)
…
end
i hope it works now ๐
Yup, works beautifully.
Thanks for the quick reply.
Btw,
Something like this should really be in rspec itself. I wonder if he would consider adding your plugin as a patch?
expected to render “new”, got nil
is not a cluefull fail message, ๐
I finally got these 2 examples that had been bugging me with to pass.
Thanks!
good to hear ๐
exactly the reason why i wrote it, always got clueless messages, when i knew only some validation crashed
I am getting the following error while executing a controller spec. what may be the reason
1)
RuntimeError in ‘LoginController handling GET /login/add should be successful’
@controller is nil: make sure you set it in your test’s setup method.
./login_controller_spec.rb:8:in `do_get’
./login_controller_spec.rb:19:
2)
NameError in ‘LoginController handling GET /login/add should render 202 file’
undefined local variable or method `response’ for #
./login_controller_spec.rb:24:
As far as i can see it means that something is wrong with your test setup
response should always be defined after a request, and @controller should always be defined in a controller test
are you sure this test passes without reponse_enhancers ?
There are some issues with the name in my current setup (edge rails, edge rspec).
The issue is that your namespace is RSpec, but the file names are rspec, rather than r_spec.
I hack-patched this by just changing the name of the plugin folder to r_spec…, and the name of the plugin file to r_spec_response_enhancers.rb
Not sure if this is an issue why my setup, or w/ your code.
Thanks again for the plugin.
As per above i had changed the file name to r_spec_response_enchancers.rb
same for the pluggin folder.
Also i added the line
config.include(RSpecResponseEnhancer) in spec helper.
But when i run the spec i get the error
@controller is nil: make sure you set it in your test’s setup method.
./user1_spec.rb:12:
it is pointing the error in my model spec and my code at that line is
delete :destroy, :id => 1
Please advice… ๐
did you try to install the most-current version from github, imo i fixed this bug a long time ago…
@pragmatig
Yes i have installed the Git(version 1.5.6.1-preview20080701)
Still i am getting the same error. ๐ฆ
the class is called RspecResponseEnhancer, so it should be found correctly, maybe the call in spec_helper is outdated RSpecResponseEnhancer ?
Thanks for the tip!
Maybe they changed the resolving from RSpec -> rspec to RSpec-> r_spec
but then is HTMLHelper == h_t_m_l_helper ?
ill google a bit to find out…