Forum: RSpec Fixing "expected success? to return true, got false"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2009-01-07 02:27
(Received via mailing list)
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
Cdf378de2284d8acf137122e541caa28?d=identicon&s=25 Matt Wynne (mattwynne)
on 2009-01-07 12:56
(Received via mailing list)
On 7 Jan 2009, at 01:25, Mark A. Richman wrote:

> ## patients_controller_spec.rb
>     @user  = mock_user
>   end
>       response.should be_success # this is the offending line 27
>
>     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
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2009-01-07 14:36
(Received via mailing list)
Matt,

Thank you very much. It turns out that a pair of
authentication/authorization before_filters were interfering with the
test.
I got past it now by doing this:

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')
    User.stub!(:in_role?).with('limited').and_return(false)
    controller_bypass_authentication(@user)
end

I've got 3 roles in my app (admin, normal, and limited), in case you
were
curious about that. I didn't know about the hash trick. Thanks!

- Mark
994e42bda994be2cd1d791f18ee6d561?d=identicon&s=25 Stephen Eley (Guest)
on 2009-01-07 18:59
(Received via mailing list)
On Wed, Jan 7, 2009 at 6:36 AM, Matt Wynne <matt@mattwynne.net> wrote:
>
> 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.

You can also check the logfiles.  I often keep two terminals running
in the background whenever I'm doing BDD coding; one's running
autospec, and the other one has 'tail -f log/test.log'.  I don't have
to look at that all the time, but when something breaks and I can't
figure out why it's a huge time saver.  I also found this post of Ben
Mabey's to be very useful for figuring out which chunk of log goes
with which spec:

http://www.benmabey.com/2008/07/04/global-setup-in...



--
Have Fun,
   Steve Eley (sfeley@gmail.com)
   ESCAPE POD - The Science Fiction Podcast Magazine
   http://www.escapepod.org
A7263d0aad8f750b83d52b6a58a44b4b?d=identicon&s=25 Mark Richman (mrichman)
on 2009-01-07 19:18
(Received via mailing list)
Steve,

That's really useful! Thank you.

- Mark
This topic is locked and can not be replied to.