Skip to content

Spying on objects without pre-defining any expectations #13

@dougalcorn

Description

@dougalcorn

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)>'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions