Jonathan Stott
2007-Jul-30 14:15 UTC
[mocha-developer] Object.stubs doesn''t seem to work.
Hi I''m using Mocha 0.5.3 and I want to stub out a call to Time.now, just like the example in the post http://blog.floehopper.org/articles/2007/06/08/mocha-0-5-released However, trying it in irb gets me the following error:>> require ''mocha''=> true>> Time.stubs(:now).returns(Time.parse(''Thu Feb 01 00:00:00 UTC 2007''))NoMethodError: undefined method `stub'' for nil:NilClass from /usr/lib/ruby/gems/1.8/gems/mocha-0.5.3/lib/mocha/object.rb:57:in `stubs'' from (irb):2 Has this way of stubbing been depreciated between 0.5.0 and 0.5.3, and if so, what is the new way of stubbing for an Object.method ? Regards Jonathan
Hi Jonathan, You need to use Mocha in the context of a Test::Unit::TestCase (or an RSpec specification). The blog post missed this out to improve the signal-to-noise ratio. require ''rubygems'' require ''mocha'' require ''test/unit'' class TimeTest < Test::Unit::TestCase def test_time_now Time.stubs(:now).returns(Time.parse(''Thu Feb 01 00:00:00 UTC 2007'')) assert_equal Time.parse(''Thu Feb 01 00:00:00 UTC 2007''), Time.now end end The reason it needs to be within a TestCase or similar is because Mocha temporarily modifies the Time class for the duration of the test and needs a hook to put it back how it was. I hope that makes sense. -- James. http://blog.floehopper.org
Jonathan Stott
2007-Jul-31 09:56 UTC
[mocha-developer] Object.stubs doesn''t seem to work.
Ah, yes, thank you! I feel a little foolish now. It hadn''t occured to me it needed to be in a TestCase when I set out to test it a little first in irb. Thanks for the quick response Jonathan
Hi, I''m trying to stub the login_required() method of the acts_as_authenticated plugin in an RoR integration test. This method is defined in the AuthenticatedSystem module, in the lib/authenticated_system.rb What happen is that the stub have no effect and the real method is still executed. Here is the code. Any idea ? Thank you require "#{File.dirname(__FILE__)}/../test_helper" class CheckStopsiteTest < ActionController::IntegrationTest fixtures :users def test_closed_site AuthenticatedSystem.stubs(:login_required).returns(true) get ''/admin/maintenance/stop_site'' assert_response :success get ''/fr/site'' assert_redirected_to :controller => ''site'', :action => ''closed_site'' end end
SiteController.any_instance.stubs(:login_required).returns(true) perhaps? On 9/27/07, Christophe <anaema_ml at yahoo.fr> wrote:> Hi, I''m trying to stub the login_required() method of the > acts_as_authenticated plugin in an RoR integration test. > This method is defined in the AuthenticatedSystem module, in the > lib/authenticated_system.rb > > What happen is that the stub have no effect and the real method is still > executed. > > Here is the code. Any idea ? > > Thank you > > > > require "#{File.dirname(__FILE__)}/../test_helper" > > class CheckStopsiteTest < ActionController::IntegrationTest > fixtures :users > > def test_closed_site > AuthenticatedSystem.stubs(:login_required).returns(true) > get ''/admin/maintenance/stop_site'' > assert_response :success > get ''/fr/site'' > assert_redirected_to :controller => ''site'', :action => ''closed_site'' > end > > end > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer >
Unfortunately I already tried this too - And it doesn''t work... le 28/09/2007 01:10, Duncan Beevers nous a dit:> SiteController.any_instance.stubs(:login_required).returns(true) > > perhaps? > > On 9/27/07, Christophe <anaema_ml at yahoo.fr> wrote: > >> Hi, I''m trying to stub the login_required() method of the >> acts_as_authenticated plugin in an RoR integration test. >> This method is defined in the AuthenticatedSystem module, in the >> lib/authenticated_system.rb >> >> What happen is that the stub have no effect and the real method is still >> executed. >> >> Here is the code. Any idea ? >> >> Thank you >> >> >> >> require "#{File.dirname(__FILE__)}/../test_helper" >> >> class CheckStopsiteTest < ActionController::IntegrationTest >> fixtures :users >> >> def test_closed_site >> AuthenticatedSystem.stubs(:login_required).returns(true) >> get ''/admin/maintenance/stop_site'' >> assert_response :success >> get ''/fr/site'' >> assert_redirected_to :controller => ''site'', :action => ''closed_site'' >> end >> >> end >>
On 27/09/2007, Christophe <anaema_ml at yahoo.fr> wrote:> > Hi, I''m trying to stub the login_required() method of the > acts_as_authenticated plugin in an RoR integration test. > This method is defined in the AuthenticatedSystem module, in the > lib/authenticated_system.rb > > What happen is that the stub have no effect and the real method is still > executed. > > Here is the code. Any idea ? > > Thank you > > > > require "#{File.dirname(__FILE__)}/../test_helper" > > class CheckStopsiteTest < ActionController::IntegrationTest > fixtures :users > > def test_closed_site > AuthenticatedSystem.stubs(:login_required).returns(true) > get ''/admin/maintenance/stop_site'' > assert_response :success > get ''/fr/site'' > assert_redirected_to :controller => ''site'', :action => ''closed_site'' > end > > endSorry not to reply sooner - I''ve been away on holiday. It''s a while since I looked at acts_as_authenticated, but I suspect the problem is that the login_required method is a module *instance* method and not a module method. This means you need to stub the method on the instance into which it has been mixed - probably the controller in this case. The difference between stubbing a module method and a module instance method is probably best explained by a couple of quick examples - see http://pastie.caboo.se/103234. In your test, I suspect you need to do something like... def test_closed_site @controller.stubs(:login_required).returns(true) get ''/admin/maintenance/stop_site'' assert_response :success get ''/fr/site'' assert_redirected_to :controller => ''site'', :action => ''closed_site'' end I hope that solves your problem. Let me know if not. One last thought - are you sure it makes sense to stub within an "integration" test? -- James. http://blog.floehopper.org http://tumble.floehopper.org