RSpec response.should Information Enhancers

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!

Advertisements

16 thoughts on “RSpec response.should Information Enhancers

  1. 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:

  2. 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 🙂

  3. 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, 🙂

  4. 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:

  5. 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 ?

  6. 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.

  7. 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… 🙂

      • @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 ?

  8. 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…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s