Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Help tracing why controller spec is failing

I have a user_controller_spec.rb that is failing, and I'm not sure why.

require 'spec_helper'

describe UsersController do

  describe "GET 'index'" do
    it "should be successful" do
      get 'index'
      response.should be_success
    end
  end


end

When I run rspec it says:

Failures:

  1) UsersController GET 'index' should be successful
     Failure/Error: response.should be_success
       expected success? to return true, got false
     # ./spec/controllers/users_controller_spec.rb:8

Finished in 0.17047 seconds
1 example, 1 failure

Going to the /home/ page in the browser works fine.

Is there a way to get a more detailed reason why it is failing?

Note:

This is rails3, and I am using rspec.

I also have the capybara gem, and searching my solution shows the only reference to capybara is in my gem and gem.lock file.

like image 949
Blankman Avatar asked May 18 '11 19:05

Blankman


2 Answers

You can try outputting the response body to see what the message is. Could be anything from the user you're logged in as not having the correct permissions (or visiting a page anonymously that you must be logged in to see) to a strange view error in test environment.

get 'index'
puts response.body.inspect
puts response.status.inspect
...
response.should be_success

response.body will contain the HTML output of the response, so you should be able to tell why it's not a success (hopefully it will have a stack trace or be a redirect or something). Also keep in mind redirecting is not "success". If I remember correctly be_success makes sure the HTTP status code is one of the 200s, redirects are usually 302 or 304 so do not count. If a redirect is intended, try response.should be_redirect.

like image 65
Brett Bender Avatar answered Oct 14 '22 06:10

Brett Bender


It could be that you do not just render the page, but redirect. To check on what may be wrong, i would do in my spec something like :

response.should == 1

in order to see what the actual response is. This would give you a good clue on what is happening.

like image 39
Spyros Avatar answered Oct 14 '22 06:10

Spyros