flexmock
flexmock copied to clipboard
Spying on objects without pre-defining any expectations
I would expect to be able to define a partial mock (i.e. flexmock(realobject)
), use the object and then test against what messages were sent to the real object. Here's an example of what I mean using a flexmock on a class:
require 'spec_helper'
class Foo
class << self
attr_accessor :track_this_value
end
end
describe "spying and passing through" do
before { flexmock(Foo) }
subject { Foo }
before { Foo.track_this_value = "baz" }
its(:track_this_value) { should == "baz" } # it passes through
it "records methods on the mocked class" do
Foo.should have_received(:track_this_value)
end
end
The test does pass the its(:track_this_value)
expectation, but not the should have_received
. Instead I get:
1) spying and passing through records methods on the mocked class
Failure/Error: Foo.should have_received(:track_this_value)
expected track_this_value(...) to be received by Foo.
No messages have been received
# ./spec/spy_spec.rb:16:in `block (2 levels) in <top (required)>'
I haven't responded to this, and I apologize for this.
I am a little worried about performance. If we spy on every method, then we have to proxy every method on the object. To do that by default means that every partial object gets that overhead, even if they never take advantage of the spy calls.
Perhaps we can flag those mocks that need to have all their methods spied upon. Something like flexmock(Foo, :fullspy) (or :spyall, or :mole, or .... something).
Thoughts? Meanwhile, I'll investigate the performance impact and see if my concerns are valid.