Jed Hurt
2008-Apr-01 01:23 UTC
[rspec-users] Stub an instance method for every new instance of a class
Is there an easy way to stub an instance method for every new instance of a given class? I suppose I could just redefine the method, but I''m wondering if the RSpec mocking framework has this built in. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080331/eadaf6aa/attachment.html
Ashley Moran
2008-Apr-01 07:50 UTC
[rspec-users] Stub an instance method for every new instance of a class
On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote:> > Is there an easy way to stub an instance method for every new instance of > a given class? I suppose I could just redefine the method, but I''m wondering > if the RSpec mocking framework has this built in.Hi Jed I hate it when other people ask this, but I can''t help :) How come you need to do this? I''ve always wondered what the use case for this "mocking" strategy is Ashley -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080401/38d11038/attachment-0001.html
Glenn Ford
2008-Apr-01 12:21 UTC
[rspec-users] Stub an instance method for every new instance of a class
I think the general consensus is that if you need this feature, then you have ugly code that needs cleaning up :) If you''re in a situation like me, where you''re too noob to figure out a better solution for some legacy code that you inherited, go to this url and find the instructions for installing Mocha: http://wiki.rubyonrails.org/rails/pages/Testing+Plugins With it you can do: ModelName.any_instance.stubs(:the_method).returns(happy_times) However, I strongly encourage you to stare at your current code for a while and try to find a cleaner MVC-supportive solution. The general rule of thumb is that if it''s difficult to write a spec for, then your code is ugly :) Hopefully in this case you can take this as an opportunity to improve your code. If not, Mocha works fairly well! Glenn On Apr 1, 2008, at 3:50 AM, Ashley Moran wrote:> On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > Is there an easy way to stub an instance method for every new > instance of a given class? I suppose I could just redefine the > method, but I''m wondering if the RSpec mocking framework has this > built in. > > Hi Jed > > I hate it when other people ask this, but I can''t help :) How come > you need to do this? I''ve always wondered what the use case for > this "mocking" strategy is > > Ashley > _______________________________________________ > 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/20080401/d65e1f0f/attachment.html
Jed Hurt
2008-Apr-01 17:19 UTC
[rspec-users] Stub an instance method for every new instance of a class
Haha, that''s about the response I was expecting; I asked with some trepidation. I''m just looking for a quick hack to short-circuit Merb''s render method to do some view/controller isolation testing until Merb officially supports it. I can''t just stub methods on @controller (like in Rails) because the controller is instantiated after calling dispatch. I suppose I could whip up a different sort of hack to accomplish the isolation in the meantime, but now I''m not so sure; apparently I''m quite a noob ;) On Tue, Apr 1, 2008 at 1:50 AM, Ashley Moran <ashley.moran at patchspace.co.uk> wrote:> On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > > > Is there an easy way to stub an instance method for every new instance > > of a given class? I suppose I could just redefine the method, but I''m > > wondering if the RSpec mocking framework has this built in. > > > Hi Jed > > I hate it when other people ask this, but I can''t help :) How come you > need to do this? I''ve always wondered what the use case for this "mocking" > strategy is > > Ashley > > _______________________________________________ > 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/20080401/891520b2/attachment.html
Corey Haines
2008-Apr-01 18:11 UTC
[rspec-users] Stub an instance method for every new instance of a class
Could you stub out Controller.new and return your mock? On Tue, Apr 1, 2008 at 1:19 PM, Jed Hurt <jed.hurt at gmail.com> wrote:> Haha, that''s about the response I was expecting; I asked with some > trepidation. I''m just looking for a quick hack to short-circuit Merb''s > render method to do some view/controller isolation testing until Merb > officially supports it. I can''t just stub methods on @controller (like in > Rails) because the controller is instantiated after calling dispatch. > I suppose I could whip up a different sort of hack to accomplish the > isolation in the meantime, but now I''m not so sure; apparently I''m quite a > noob ;) > > On Tue, Apr 1, 2008 at 1:50 AM, Ashley Moran < > ashley.moran at patchspace.co.uk> wrote: > > > On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > > Is there an easy way to stub an instance method for every new instance > > > of a given class? I suppose I could just redefine the method, but I''m > > > wondering if the RSpec mocking framework has this built in. > > > > > > Hi Jed > > > > I hate it when other people ask this, but I can''t help :) How come you > > need to do this? I''ve always wondered what the use case for this "mocking" > > strategy is > > > > Ashley > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- http://www.coreyhaines.com The Internet''s Premiere source of information about Corey Haines -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080401/53ada359/attachment.html
David Chelimsky
2008-Apr-01 18:12 UTC
[rspec-users] Stub an instance method for every new instance of a class
On Tue, Apr 1, 2008 at 2:11 PM, Corey Haines <coreyhaines at gmail.com> wrote:> Could you stub out Controller.new and return your mock?Bingo.> > > > On Tue, Apr 1, 2008 at 1:19 PM, Jed Hurt <jed.hurt at gmail.com> wrote: > > Haha, that''s about the response I was expecting; I asked with some > trepidation. I''m just looking for a quick hack to short-circuit Merb''s > render method to do some view/controller isolation testing until Merb > officially supports it. I can''t just stub methods on @controller (like in > Rails) because the controller is instantiated after calling dispatch. > > > > > > I suppose I could whip up a different sort of hack to accomplish the > isolation in the meantime, but now I''m not so sure; apparently I''m quite a > noob ;) > > > > > > > > > > > > On Tue, Apr 1, 2008 at 1:50 AM, Ashley Moran > <ashley.moran at patchspace.co.uk> wrote: > > > > > > > > > > > > > > > > > On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > > > > > > > Is there an easy way to stub an instance method for every new instance > of a given class? I suppose I could just redefine the method, but I''m > wondering if the RSpec mocking framework has this built in. > > > > > > > > > Hi Jed > > > > > > I hate it when other people ask this, but I can''t help :) How come you > need to do this? I''ve always wondered what the use case for this "mocking" > strategy is > > > > > > Ashley > > > > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > -- > http://www.coreyhaines.com > The Internet''s Premiere source of information about Corey Haines > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Jed Hurt
2008-Apr-02 04:22 UTC
[rspec-users] Stub an instance method for every new instance of a class
I''m not sure how that would work. For controller specs, I want to keep most of the controller intact so that I can verify its actual behavior. Am I missing something? On Tue, Apr 1, 2008 at 12:12 PM, David Chelimsky <dchelimsky at gmail.com> wrote:> On Tue, Apr 1, 2008 at 2:11 PM, Corey Haines <coreyhaines at gmail.com> > wrote: > > Could you stub out Controller.new and return your mock? > > Bingo. > > > > > > > > > On Tue, Apr 1, 2008 at 1:19 PM, Jed Hurt <jed.hurt at gmail.com> wrote: > > > Haha, that''s about the response I was expecting; I asked with some > > trepidation. I''m just looking for a quick hack to short-circuit Merb''s > > render method to do some view/controller isolation testing until Merb > > officially supports it. I can''t just stub methods on @controller (like > in > > Rails) because the controller is instantiated after calling dispatch. > > > > > > > > > I suppose I could whip up a different sort of hack to accomplish the > > isolation in the meantime, but now I''m not so sure; apparently I''m quite > a > > noob ;) > > > > > > > > > > > > > > > > > > On Tue, Apr 1, 2008 at 1:50 AM, Ashley Moran > > <ashley.moran at patchspace.co.uk> wrote: > > > > > > > > > > > > > > > > > > > > > > > On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > > > > > > > > > > Is there an easy way to stub an instance method for every new > instance > > of a given class? I suppose I could just redefine the method, but I''m > > wondering if the RSpec mocking framework has this built in. > > > > > > > > > > > > Hi Jed > > > > > > > > I hate it when other people ask this, but I can''t help :) How come > you > > need to do this? I''ve always wondered what the use case for this > "mocking" > > strategy is > > > > > > > > Ashley > > > > > > > > > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > -- > > http://www.coreyhaines.com > > The Internet''s Premiere source of information about Corey Haines > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > _______________________________________________ > 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/20080401/7e954f23/attachment.html
Corey Haines
2008-Apr-02 14:19 UTC
[rspec-users] Stub an instance method for every new instance of a class
Create an instance of the controller contr = MyController.new MyController.stub!(:new).and_return contr contr.stub!(:method_i_want_to_stub).and_return ''sweetness'' Something like this? -Corey On Wed, Apr 2, 2008 at 12:22 AM, Jed Hurt <jed.hurt at gmail.com> wrote:> I''m not sure how that would work. For controller specs, I want to keep > most of the controller intact so that I can verify its actual behavior. Am I > missing something? > > > On Tue, Apr 1, 2008 at 12:12 PM, David Chelimsky <dchelimsky at gmail.com> > wrote: > > > On Tue, Apr 1, 2008 at 2:11 PM, Corey Haines <coreyhaines at gmail.com> > > wrote: > > > Could you stub out Controller.new and return your mock? > > > > Bingo. > > > > > > > > > > > > > > On Tue, Apr 1, 2008 at 1:19 PM, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > Haha, that''s about the response I was expecting; I asked with some > > > trepidation. I''m just looking for a quick hack to short-circuit Merb''s > > > render method to do some view/controller isolation testing until Merb > > > officially supports it. I can''t just stub methods on @controller (like > > in > > > Rails) because the controller is instantiated after calling dispatch. > > > > > > > > > > > > I suppose I could whip up a different sort of hack to accomplish the > > > isolation in the meantime, but now I''m not so sure; apparently I''m > > quite a > > > noob ;) > > > > > > > > > > > > > > > > > > > > > > > > On Tue, Apr 1, 2008 at 1:50 AM, Ashley Moran > > > <ashley.moran at patchspace.co.uk> wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > > > > > > > > > > > > > Is there an easy way to stub an instance method for every new > > instance > > > of a given class? I suppose I could just redefine the method, but I''m > > > wondering if the RSpec mocking framework has this built in. > > > > > > > > > > > > > > > Hi Jed > > > > > > > > > > I hate it when other people ask this, but I can''t help :) How > > come you > > > need to do this? I''ve always wondered what the use case for this > > "mocking" > > > strategy is > > > > > > > > > > Ashley > > > > > > > > > > > > > > > _______________________________________________ > > > > > rspec-users mailing list > > > > > rspec-users at rubyforge.org > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > -- > > > http://www.coreyhaines.com > > > The Internet''s Premiere source of information about Corey Haines > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- http://www.coreyhaines.com The Internet''s Premiere source of information about Corey Haines -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/rspec-users/attachments/20080402/9e2fa221/attachment.html
Jed Hurt
2008-Apr-03 04:29 UTC
[rspec-users] Stub an instance method for every new instance of a class
Ahhh, clever. I see the light On Wed, Apr 2, 2008 at 8:19 AM, Corey Haines <coreyhaines at gmail.com> wrote:> Create an instance of the controller > > contr = MyController.new > > MyController.stub!(:new).and_return contr > > contr.stub!(:method_i_want_to_stub).and_return ''sweetness'' > > Something like this? > > -Corey > > On Wed, Apr 2, 2008 at 12:22 AM, Jed Hurt <jed.hurt at gmail.com> wrote: > > > I''m not sure how that would work. For controller specs, I want to keep > > most of the controller intact so that I can verify its actual behavior. Am I > > missing something? > > > > > > On Tue, Apr 1, 2008 at 12:12 PM, David Chelimsky <dchelimsky at gmail.com> > > wrote: > > > > > On Tue, Apr 1, 2008 at 2:11 PM, Corey Haines <coreyhaines at gmail.com> > > > wrote: > > > > Could you stub out Controller.new and return your mock? > > > > > > Bingo. > > > > > > > > > > > > > > > > > > > On Tue, Apr 1, 2008 at 1:19 PM, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > > Haha, that''s about the response I was expecting; I asked with some > > > > trepidation. I''m just looking for a quick hack to short-circuit > > > Merb''s > > > > render method to do some view/controller isolation testing until > > > Merb > > > > officially supports it. I can''t just stub methods on @controller > > > (like in > > > > Rails) because the controller is instantiated after calling > > > dispatch. > > > > > > > > > > > > > > > I suppose I could whip up a different sort of hack to accomplish > > > the > > > > isolation in the meantime, but now I''m not so sure; apparently I''m > > > quite a > > > > noob ;) > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On Tue, Apr 1, 2008 at 1:50 AM, Ashley Moran > > > > <ashley.moran at patchspace.co.uk> wrote: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > On 01/04/2008, Jed Hurt <jed.hurt at gmail.com> wrote: > > > > > > > > > > > > > > > > > > > Is there an easy way to stub an instance method for every new > > > instance > > > > of a given class? I suppose I could just redefine the method, but > > > I''m > > > > wondering if the RSpec mocking framework has this built in. > > > > > > > > > > > > > > > > > > Hi Jed > > > > > > > > > > > > I hate it when other people ask this, but I can''t help :) How > > > come you > > > > need to do this? I''ve always wondered what the use case for this > > > "mocking" > > > > strategy is > > > > > > > > > > > > Ashley > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > > rspec-users mailing list > > > > > > rspec-users at rubyforge.org > > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > rspec-users mailing list > > > > > rspec-users at rubyforge.org > > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > > > > > > > > > > > > > -- > > > > http://www.coreyhaines.com > > > > The Internet''s Premiere source of information about Corey Haines > > > > _______________________________________________ > > > > rspec-users mailing list > > > > rspec-users at rubyforge.org > > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > _______________________________________________ > > > rspec-users mailing list > > > rspec-users at rubyforge.org > > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > > > > _______________________________________________ > > rspec-users mailing list > > rspec-users at rubyforge.org > > http://rubyforge.org/mailman/listinfo/rspec-users > > > > > > -- > http://www.coreyhaines.com > The Internet''s Premiere source of information about Corey Haines > > _______________________________________________ > 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/20080402/6a9b6bed/attachment.html