Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spying on objects without pre-defining any expectations #13

Open
dougalcorn opened this issue Jan 29, 2013 · 1 comment
Open

Spying on objects without pre-defining any expectations #13

dougalcorn opened this issue Jan 29, 2013 · 1 comment

Comments

@dougalcorn
Copy link

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)>'
@jimweirich
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants