Mark A. Richman
2009-Jan-07 01:25 UTC
[rspec-users] Fixing "expected success? to return true, got false"
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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090106/431b335d/attachment.html>
Matt Wynne
2009-Jan-07 11:36 UTC
[rspec-users] Fixing "expected success? to return true, got false"
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/354313I 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
Mark A. Richman
2009-Jan-07 12:55 UTC
[rspec-users] Fixing "expected success? to return true, got false"
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 On Wed, Jan 7, 2009 at 6:36 AM, Matt Wynne <matt at mattwynne.net> wrote:> 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 > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090107/a345ff12/attachment-0001.html>
Stephen Eley
2009-Jan-07 17:40 UTC
[rspec-users] Fixing "expected success? to return true, got false"
On Wed, Jan 7, 2009 at 6:36 AM, Matt Wynne <matt at 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-rspec-or-how-to-add-logging-for-specs/ -- Have Fun, Steve Eley (sfeley at gmail.com) ESCAPE POD - The Science Fiction Podcast Magazine http://www.escapepod.org
Mark A. Richman
2009-Jan-07 18:05 UTC
[rspec-users] Fixing "expected success? to return true, got false"
Steve, That''s really useful! Thank you. - Mark On Wed, Jan 7, 2009 at 12:40 PM, Stephen Eley <sfeley at gmail.com> wrote:> On Wed, Jan 7, 2009 at 6:36 AM, Matt Wynne <matt at 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-rspec-or-how-to-add-logging-for-specs/ > > > > -- > Have Fun, > Steve Eley (sfeley at gmail.com) > ESCAPE POD - The Science Fiction Podcast Magazine > http://www.escapepod.org > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20090107/5e7882e6/attachment.html>