From jamesmead44 at gmail.com Wed Oct 3 04:40:06 2007 From: jamesmead44 at gmail.com (James Mead) Date: Wed, 3 Oct 2007 09:40:06 +0100 Subject: [mocha-developer] Stubbing a module method In-Reply-To: <46FC31FF.5050605@yahoo.fr> References: <14cf210a0707300715i49a9f04dw4f716593e66e663b@mail.gmail.com> <1db558f00707300727s65483d58v273cc6e74514042a@mail.gmail.com> <46FC31FF.5050605@yahoo.fr> Message-ID: <1db558f00710030140r594c5c96k7ac7d4e98765cba2@mail.gmail.com> On 27/09/2007, Christophe 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 Sorry 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 From jamesmead44 at gmail.com Thu Oct 4 17:57:26 2007 From: jamesmead44 at gmail.com (James Mead) Date: Thu, 4 Oct 2007 22:57:26 +0100 Subject: [mocha-developer] Warning - internal refactoring in progress Message-ID: <1db558f00710041457y2cdeec10p646bcff3056cf4ea@mail.gmail.com> Just in case anybody is bold enough to be using un-pistonized "edge" Mocha, I thought I would warn you that I'm doing some moderately significant internal refactoring. Hopefully this should be invisible, but there is a chance I might accidentally break something. Let me know if you have any problems and I'll try and sort it out. -- James. http://blog.floehopper.org http://tumble.floehopper.org From jamesmead44 at gmail.com Sun Oct 7 12:47:35 2007 From: jamesmead44 at gmail.com (James Mead) Date: Sun, 7 Oct 2007 17:47:35 +0100 Subject: [mocha-developer] Supressing RDoc links Message-ID: <1db558f00710070947r136c03dbva301447e866f01f8@mail.gmail.com> I recently noticed some anomalies in the Mocha RDoc. Within a method RDoc comment, any word that happens to be the same as a method in the same class gets turned into a link. You can see an example [2] with the word "once" and another [3] with the words "yields", "times" and "with". Does anyone know how to stop these words incorrectly being made into hyper-links...? Thanks. -- James. http://blog.floehopper.org http://tumble.floehopper.org [1] http://mocha.rubyforge.org [2] http://mocha.rubyforge.org/classes/Mocha/Expectation.html#M000021 [3] http://mocha.rubyforge.org/classes/Mocha/Expectation.html#M000026 From jamesmead44 at gmail.com Sat Oct 13 13:41:03 2007 From: jamesmead44 at gmail.com (James Mead) Date: Sat, 13 Oct 2007 18:41:03 +0100 Subject: [mocha-developer] Parameter Matchers with optional params In-Reply-To: <208AEAF5-67E1-44F9-A216-05DB0F0B09A2@avit.ca> References: <1db558f00709240311n3b35d980v3246563b16d5c637@mail.gmail.com> <381AB591-B425-4E66-8814-DCA6DC3C6BA4@gmail.com> <1db558f00709250137t36e88519o828f8785b4b55fa5@mail.gmail.com> <1db558f00709250140k15d0ca45o89d0cdb88fa241b0@mail.gmail.com> <208AEAF5-67E1-44F9-A216-05DB0F0B09A2@avit.ca> Message-ID: <1db558f00710131041kf5206b2jf82fd4751bccb17c@mail.gmail.com> Personally I don't tend to do this very much, but I don't see anything particularly "wrong" with it. I think the RSpec mocking framework has something built in for doing something similar for ActiveRecord objects. On 25/09/2007, Andrew Vit wrote: > > Sounds great... I'm all for syntactic sugar. > > I'm pretty new to TDD and especially to mocking, so I'm sure I'm > overreaching a bit for what I'm trying to do. > > Do you guys ever use any helper methods to set up pre-fab mock > objects for your test cases? Because that's what I'm trying to do for > my ActiveRecord models, and it was tripping up depending an the exact > parameters of the find method, hence my original question: > > module Factory > > def self.method_missing( name, *args ) > # returns either a plain ActiveRecord Model.new with sensible, > overrideable > # model attributes, (not shown for clarity) or runs it through > # self.proxy to make a mock if the method name has /^proxy_/ > end > > # give me a model that's "saved" to satisfy associations: > > def self.proxy( obj, obj_id ) > obj.stubs(:id).returns obj_id > obj.stubs(:valid?).returns true > obj.stubs(:new_record?).returns false > obj.class.stubs(:find).with(){ |*args| args[0].class == > Fixnum }.returns obj > obj > end > > end > > I found I was using the above pattern a lot, to stub out saved AR > objects to satisfy model associations, so I tried to abstract it into > this helper. However, I sometimes call the find directly with just > find(id), and the rest of the time it's called by ActiveRecord to > validate the association, and that's why I needed to match & ignore > any extra params. > > --Andrew > > > > On Sep 25, 2007, at 1:40 AM, James Mead wrote: > > > Andrew - Sorry. I also meant to say that I am confident that any > > replacement > > for this style of matching will be versatile enough. I have two or > > three > > ideas knocking around at the moment. > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer > -- James. http://blog.floehopper.org http://tumble.floehopper.org From jamesmead44 at gmail.com Mon Oct 15 08:02:25 2007 From: jamesmead44 at gmail.com (James Mead) Date: Mon, 15 Oct 2007 13:02:25 +0100 Subject: [mocha-developer] Fwd: Content filtered message notification In-Reply-To: References: Message-ID: <1db558f00710150502p583fbf46h832f355d29567498@mail.gmail.com> I'm not sure why mailman bounced your message (perhaps because you are not subscribed to the mailing list), but I'm glad you have solved your problem. Cheers, James. ---------- Forwarded message ---------- From: mailman-bounces at rubyforge.org Date: 14 Oct 2007 17:21 Subject: Content filtered message notification To: mocha-developer-owner at rubyforge.org The attached message matched the mocha-developer mailing list's content filtering rules and was prevented from being forwarded on to the list membership. You are receiving the only remaining copy of the discarded message. ---------- Forwarded message ---------- From: Christophe To: mocha-developer at rubyforge.org Date: Sun, 14 Oct 2007 18:15:09 +0200 Subject: Re: [mocha-developer] Stubbing a module method le 03/10/2007 10:40, James Mead nous a dit: On 27/09/2007, Christophe 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 Sorry 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? Thank you for your answer James (I missed it in the mailing list, so I only reply now). You're right, I should have no need to stub this method within an integration test ! But I had few problems with the login process and (because I'm lazy) I tought it would be easier to stub the login_required method. Yes it's make more sense to stub the controller's method, but unfortunately under an integration test you get a controller instance AFTER the first request (the @controller does not exist as in functionnal tests), so stubbing can't work here. But today, I've rewritten the integration test and I no more need to stubs the login method, I followed the right path ;-) Regards Christophe Gimenez -- James. http://blog.floehopper.org http://tumble.floehopper.org From jamesmead44 at gmail.com Thu Oct 25 09:04:11 2007 From: jamesmead44 at gmail.com (James Mead) Date: Thu, 25 Oct 2007 14:04:11 +0100 Subject: [mocha-developer] Fwd: [ mocha-Feature Requests-15021 ] without or not params matcher In-Reply-To: <20071025130137.BF808185866A@rubyforge.org> References: <20071025130137.BF808185866A@rubyforge.org> Message-ID: <1db558f00710250604x64d7511ev9c79107db90ea74f@mail.gmail.com> ---------- Forwarded message ---------- From: noreply at rubyforge.org Date: 25 Oct 2007 14:01 Subject: [ mocha-Feature Requests-15021 ] without or not params matcher To: noreply at rubyforge.org Feature Requests item #15021, was opened at 2007-10-24 15:33 You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=7480&aid=15021&group_id=1917 Category: Interface Improvements (example) Group: None Status: Open Priority: 3 Submitted By: Jerry Vos (jerryvos) Assigned to: Nobody (None) Summary: without or not params matcher Initial Comment: Would be nice if you could do MyClass.expects(:create!).without(hasKey(:property1)) or something like MyClass.expects(:create!).with(not(hasKey(:property1))) MyClass.expects(:create!).with(not.hasKey(:property1))) But you can't use not because not is a ruby keyword. The Not Matcher is just something like: class Not # :nodoc: def initialize(matcher) @matcher = matcher end def ==(parameter) !(matcher == parameter) end def mocha_inspect "not(#{@matcher.mocha_inspect})" end end ---------------------------------------------------------------------- >Comment By: James Mead (jamesmead) Date: 2007-10-25 13:01 Message: Hi Jerry, Thanks for the suggestions. I had wanted to add not() a while ago, but ran into the keyword issue. I've just committed revision 191 with Not() instead. It isn't great, but it's the best I can think of at the moment. What do you think? I don't think the without() works because you may want to negate only some of your matchers. Cheers, James. ---------------------------------------------------------------------- You can respond by visiting: http://rubyforge.org/tracker/?func=detail&atid=7480&aid=15021&group_id=1917 -- James. http://blog.floehopper.org http://tumble.floehopper.org From duncanbeevers at gmail.com Thu Oct 25 15:16:53 2007 From: duncanbeevers at gmail.com (Duncan Beevers) Date: Thu, 25 Oct 2007 12:16:53 -0700 Subject: [mocha-developer] Insure a method is called with no arguments. Message-ID: <6a2b4fd00710251216o4c9166d1q7d4b7a17b992f4eb@mail.gmail.com> We've been working with the ParameterMatchers (so cool!) but I don't see a way to insure a method is called with no arguments. I imagined something like: user = mock('User') user.expects(:friends).with(nothing) Anyone run into this? From jamesmead44 at gmail.com Thu Oct 25 17:21:08 2007 From: jamesmead44 at gmail.com (James Mead) Date: Thu, 25 Oct 2007 22:21:08 +0100 Subject: [mocha-developer] Insure a method is called with no arguments. In-Reply-To: <6a2b4fd00710251216o4c9166d1q7d4b7a17b992f4eb@mail.gmail.com> References: <6a2b4fd00710251216o4c9166d1q7d4b7a17b992f4eb@mail.gmail.com> Message-ID: <1db558f00710251421w306e9b04taf78599e8186bc6@mail.gmail.com> On 25/10/2007, Duncan Beevers wrote: > > We've been working with the ParameterMatchers (so cool!) but I don't > see a way to insure a method is called with no arguments. > > I imagined something like: > > user = mock('User') > user.expects(:friends).with(nothing) > > Anyone run into this? > _______________________________________________ > mocha-developer mailing list > mocha-developer at rubyforge.org > http://rubyforge.org/mailman/listinfo/mocha-developer > I'm glad you're finding the ParameterMatchers useful. You should be able to do what you want by using with() with no parameters. mock.expects(:method).with() Let me know if that doesn't work. In any case I quite like the nothing matcher idea, so I might add it as a more expressive alternative. Thanks -- James. http://blog.floehopper.org http://tumble.floehopper.org