David Beckwith
2008-Mar-28 18:25 UTC
[rspec-users] How do you mock an object that you don''t have access to?
Hi, How can I mock the "go" method of class B so that it returns the string "fudge" in this situation? class A private def start @b = B.new end end class B def go puts "This is fun." end end Also, is it possible to do the mocha-like any_instance thing with RSpec? Thank you, David :)
Luis Lavena
2008-Mar-28 18:32 UTC
[rspec-users] How do you mock an object that you don''t have access to?
On Fri, Mar 28, 2008 at 3:25 PM, David Beckwith <dbitsolutions at gmail.com> wrote:> Hi, > > How can I mock the "go" method of class B so that it returns the > string "fudge" in this situation? > > class A > private > def start > @b = B.new > end > end > > class B > def go > puts "This is fun." > end > end >What about: mock_b = mock(B) mock_b.stub!(:go).and_return(true) B.stub!(:new).and_return(mock_b) Something like that? -- Luis Lavena Multimedia systems - Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so. Douglas Adams
Pat Maddox
2008-Mar-28 19:12 UTC
[rspec-users] How do you mock an object that you don''t have access to?
On Fri, Mar 28, 2008 at 11:32 AM, Luis Lavena <luislavena at gmail.com> wrote:> On Fri, Mar 28, 2008 at 3:25 PM, David Beckwith <dbitsolutions at gmail.com> wrote: > > Hi, > > > > How can I mock the "go" method of class B so that it returns the > > string "fudge" in this situation? > > > > class A > > private > > def start > > @b = B.new > > end > > end > > > > class B > > def go > > puts "This is fun." > > end > > end > > > > What about: > > mock_b = mock(B) > mock_b.stub!(:go).and_return(true) > > B.stub!(:new).and_return(mock_b) > > Something like that?Stubbing the #new method on the class is one way to do it, as Luis mentioned. Another approach is to pass the object in through the constructor. Doing so will help you achieve looser coupling between the classes. Pat
aslak hellesoy
2008-Mar-28 19:17 UTC
[rspec-users] How do you mock an object that you don''t have access to?
On Fri, Mar 28, 2008 at 8:12 PM, Pat Maddox <pergesu at gmail.com> wrote:> On Fri, Mar 28, 2008 at 11:32 AM, Luis Lavena <luislavena at gmail.com> wrote: > > On Fri, Mar 28, 2008 at 3:25 PM, David Beckwith <dbitsolutions at gmail.com> wrote: > > > Hi, > > > > > > How can I mock the "go" method of class B so that it returns the > > > string "fudge" in this situation? > > > > > > class A > > > private > > > def start > > > @b = B.new > > > end > > > end > > > > > > class B > > > def go > > > puts "This is fun." > > > end > > > end > > > > > > > What about: > > > > mock_b = mock(B) > > mock_b.stub!(:go).and_return(true) > > > > B.stub!(:new).and_return(mock_b) > > > > Something like that? > > Stubbing the #new method on the class is one way to do it, as Luis mentioned. > > Another approach is to pass the object in through the constructor. > Doing so will help you achieve looser coupling between the classes. >AKA Dependency Injection (http://martinfowler.com/articles/injection.html) Aslak> Pat > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Zach Dennis
2008-Mar-28 19:33 UTC
[rspec-users] How do you mock an object that you don''t have access to?
http://atomicobjectrb.rubyforge.org/injection/ Zach On Fri, Mar 28, 2008 at 2:17 PM, aslak hellesoy <aslak.hellesoy at gmail.com> wrote:> > On Fri, Mar 28, 2008 at 8:12 PM, Pat Maddox <pergesu at gmail.com> wrote: > > On Fri, Mar 28, 2008 at 11:32 AM, Luis Lavena <luislavena at gmail.com> wrote: > > > On Fri, Mar 28, 2008 at 3:25 PM, David Beckwith <dbitsolutions at gmail.com> wrote: > > > > Hi, > > > > > > > > How can I mock the "go" method of class B so that it returns the > > > > string "fudge" in this situation? > > > > > > > > class A > > > > private > > > > def start > > > > @b = B.new > > > > end > > > > end > > > > > > > > class B > > > > def go > > > > puts "This is fun." > > > > end > > > > end > > > > > > > > > > What about: > > > > > > mock_b = mock(B) > > > mock_b.stub!(:go).and_return(true) > > > > > > B.stub!(:new).and_return(mock_b) > > > > > > Something like that? > > > > Stubbing the #new method on the class is one way to do it, as Luis mentioned. > > > > Another approach is to pass the object in through the constructor. > > Doing so will help you achieve looser coupling between the classes. > > > > AKA Dependency Injection (http://martinfowler.com/articles/injection.html) > > Aslak > > > > > Pat > > > > > > _______________________________________________ > > 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 >-- Zach Dennis http://www.continuousthinking.com
David Beckwith
2008-Mar-29 03:44 UTC
[rspec-users] How do you mock an object that you don''t have access to?
Awesome. Thank you. David :) On Fri, Mar 28, 2008 at 11:32 AM, Luis Lavena <luislavena at gmail.com> wrote:> On Fri, Mar 28, 2008 at 3:25 PM, David Beckwith <dbitsolutions at gmail.com> wrote: > > Hi, > > > > How can I mock the "go" method of class B so that it returns the > > string "fudge" in this situation? > > > > class A > > private > > def start > > @b = B.new > > end > > end > > > > class B > > def go > > puts "This is fun." > > end > > end > > > > What about: > > mock_b = mock(B) > mock_b.stub!(:go).and_return(true) > > B.stub!(:new).and_return(mock_b) > > Something like that? > > -- > Luis Lavena > Multimedia systems > - > Human beings, who are almost unique in having the ability to learn from > the experience of others, are also remarkable for their apparent > disinclination to do so. > Douglas Adams > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >-- SMS/Text: +63 905 220 2207 Skype: dbit_solutions