On Fri, Mar 28, 2008 at 8:12 PM, Pat Maddox <[EMAIL PROTECTED]> wrote: > On Fri, Mar 28, 2008 at 11:32 AM, Luis Lavena <[EMAIL PROTECTED]> wrote: > > On Fri, Mar 28, 2008 at 3:25 PM, David Beckwith <[EMAIL PROTECTED]> 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 > [email protected] > http://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ rspec-users mailing list [email protected] http://rubyforge.org/mailman/listinfo/rspec-users
