I am having trouble mocking an injected object. For example:
class Foo
def initialize(bar = Bar.new)
@bar = bar
end
def run
@bar.do_something_cool
end
end
# Rspec
describe Foo do
it "should do something cool" do
mock_bar = mock("bar")
mock_bar.stub(:do_something_cool).and_return(nil)
real_foo = Foo.new(mock_bar)
real_foo.run
mock_bar.should_receive(:do_something_cool).once
end
end
If I run this, the spec fails because it says the “do_something_cool” is never called.
expected: 1 time
received: 0 times
However, if I do not stub “do_something_cool”, I get the following error
Mock "bar" received unexpected message :do_something_cool with (no args)
Any ideas?
should be before