david
March 28, 2008, 7:28pm
#1
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
david
March 28, 2008, 7:36pm
#2
On Fri, Mar 28, 2008 at 3:25 PM, David B.
[email protected] wrote:
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 L.
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
david
March 28, 2008, 8:14pm
#3
On Fri, Mar 28, 2008 at 11:32 AM, Luis L. [email protected]
wrote:
end
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
david
March 28, 2008, 8:44pm
#4
http://atomicobjectrb.rubyforge.org/injection/
Zach
On Fri, Mar 28, 2008 at 2:17 PM, aslak hellesoy
david
March 28, 2008, 8:24pm
#6
On Fri, Mar 28, 2008 at 8:12 PM, Pat M. [email protected] wrote:
@b = B.new
What about:
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