[rspec-users] Fixing "expected success? to return true, got false"

Matt Wynne matt at mattwynne.net
Wed Jan 7 06:36:45 EST 2009


On 7 Jan 2009, at 01:25, Mark A. Richman wrote:

> I am trying to get the following test to pass, and get this error.  
> Since I'm only on day 4 of rspec, I'm sure I'm missing something  
> simple. Any ideas?
> ## rake spec
>
> 'PatientsController GET 'new' should be successful' FAILED
>
> expected success? to return true, got false
> ./spec/controllers/patients_controller_spec.rb:27:
>
> ## patients_controller_spec.rb
>
> require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
>
>
> describe PatientsController do
>
>   integrate_views
>   fixtures :all
>
>   before(:each) do
>     @user  = mock_user
>     @login_params = { :login => 'quentin', :password => 'monkey' }
>
>     User.stub!(:authenticate).with(@login_params[:login],  
> @login_params[:password]).and_return(@user)
>     @user.stub!(:enabled?).and_return(true)
>     @user.stub!(:account_id).and_return(1)
>     @user.stub!(:time_zone).and_return('Eastern Time (US & Canada)')
>
>     @user.stub!(:role).and_return('Normal')
>     controller_bypass_authentication(@user)
>   end
>
>   #Delete these examples and add some real ones
>   it "should use PatientsController" do
>
>     controller.should be_an_instance_of(PatientsController)
>   end
>
>   describe "GET 'new'" do
>     it "should be successful" do
>       get 'new'
>       response.should be_success # this is the offending line 27
>
>     end
>   end
> end
>
> # Allows a spec to bypass auth for controllers that filter for  
> logged_in user
> def controller_bypass_authentication(user=nil)
>   controller.stub!(:logged_in).and_return(true)
>   controller.stub!(:authorized?).and_return(true)
>
>   controller.stub!(:current_user).and_return(user)
> end
>
> ## patients_controller.rb
>
> ...
> def new
>   @patient = Patient.new
>
>   respond_to do |format|
>     format.html # new.html.erb
>     format.xml  { render :xml => @patient }
>
>   end
> end
> ...
> http://www.pastie.org/354313

I don't think there's enough to go on here. We need to know why the  
request failed, and it could be for any number of reasons. You've  
shown us a load of test setup code for stubbing out your  
authentication mechanism, but you haven't shown us the actual  
authentication mechanism in your controller, so we have no way of  
knowing whether your stubs are good.

I would start your debugging by adding a puts statement between lines  
26 and 27 to show more of the (unexpected) response. Looking at  
response.code or response.body should give you some clues.

By the way, did you know you can pass hashes of stubs to the mock()  
method?

You could clean up your setup code quite a bit by doing this:

@user = mock(User,
   :enabled? => true,
   :account_id => 1,
   :time_zone => 'Eastern Time (US & Canada)',
   :role => 'Normal')

HTH,

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com



More information about the rspec-users mailing list