Right now, #stub stubs a method even if it doesn''t exist. Is there a way to stub a method on an object, if and only if it already #responds_to? that method? It would also be good if it raised an ArgumentError or the like. Example: # this is how it works now: # - stubs the #bar method even if it doesn''t exist # - always returns ''baz'' foo.stub(:bar).and_return(''baz'') What I''d like is something like this: # raise ArgumentError unless foo.respond_to? :bar # otherwise, behaves as the regular #stub does foo.stub!(:bar).and_return(''baz'') Thoughts? ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/
On Jul 27, 2011, at 2:32 PM, John Feminella wrote:> Right now, #stub stubs a method even if it doesn''t exist. Is there a > way to stub a method on an object, if and only if it already > #responds_to? that method? It would also be good if it raised an > ArgumentError or the like. > > Example: > > # this is how it works now: > # - stubs the #bar method even if it doesn''t exist > # - always returns ''baz'' > foo.stub(:bar).and_return(''baz'') > > What I''d like is something like this: > > # raise ArgumentError unless foo.respond_to? :bar > # otherwise, behaves as the regular #stub does > foo.stub!(:bar).and_return(''baz'') > > Thoughts?This has come up many times before. Please check out http://groups.google.com/group/rspec/browse_thread/thread/c344c898c278cd2b and https://github.com/rspec/rspec-mocks/issues/15 to see where the conversation is at this point.
Apologies for not noticing this -- I did search, but I wasn''t sure exactly how to phrase it. Thanks for the info, David. ~ jf -- John Feminella Principal Consultant, BitsBuilder LI: http://www.linkedin.com/in/johnxf SO: http://stackoverflow.com/users/75170/ On Wed, Jul 27, 2011 at 16:05, David Chelimsky <dchelimsky at gmail.com> wrote:> On Jul 27, 2011, at 2:32 PM, John Feminella wrote: > >> Right now, #stub stubs a method even if it doesn''t exist. Is there a >> way to stub a method on an object, if and only if it already >> #responds_to? that method? It would also be good if it raised an >> ArgumentError or the like. >> >> Example: >> >> # this is how it works now: >> # ? - stubs the #bar method even if it doesn''t exist >> # ? - always returns ''baz'' >> foo.stub(:bar).and_return(''baz'') >> >> What I''d like is something like this: >> >> # raise ArgumentError unless foo.respond_to? :bar >> # otherwise, behaves as the regular #stub does >> foo.stub!(:bar).and_return(''baz'') >> >> Thoughts? > > This has come up many times before. Please check out http://groups.google.com/group/rspec/browse_thread/thread/c344c898c278cd2b and https://github.com/rspec/rspec-mocks/issues/15 to see where the conversation is at this point. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On Jul 27, 2011, at 5:36 PM, John Feminella <johnf at bitsbuilder.com> wrote:> Apologies for not noticing this -- I did search, but I wasn''t sure > exactly how to phrase it. Thanks for the info, David.No apology necessary. Just saving myself a bunch of re-typing :) Feel free to weigh in on those threads. Cheers, David> ~ jf > -- > John Feminella > Principal Consultant, BitsBuilder > LI: http://www.linkedin.com/in/johnxf > SO: http://stackoverflow.com/users/75170/ > > > > On Wed, Jul 27, 2011 at 16:05, David Chelimsky <dchelimsky at gmail.com> wrote: >> On Jul 27, 2011, at 2:32 PM, John Feminella wrote: >> >>> Right now, #stub stubs a method even if it doesn''t exist. Is there a >>> way to stub a method on an object, if and only if it already >>> #responds_to? that method? It would also be good if it raised an >>> ArgumentError or the like. >>> >>> Example: >>> >>> # this is how it works now: >>> # - stubs the #bar method even if it doesn''t exist >>> # - always returns ''baz'' >>> foo.stub(:bar).and_return(''baz'') >>> >>> What I''d like is something like this: >>> >>> # raise ArgumentError unless foo.respond_to? :bar >>> # otherwise, behaves as the regular #stub does >>> foo.stub!(:bar).and_return(''baz'') >>> >>> Thoughts? >> >> This has come up many times before. Please check out http://groups.google.com/group/rspec/browse_thread/thread/c344c898c278cd2b and https://github.com/rspec/rspec-mocks/issues/15 to see where the conversation is at this point. >> _______________________________________________ >>