Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Last active August 29, 2015 14:03
Show Gist options
  • Save jcoglan/cb8e64428d8fda89392b to your computer and use it in GitHub Desktop.
Save jcoglan/cb8e64428d8fda89392b to your computer and use it in GitHub Desktop.
# This spec works on RSpec 2.99 but not on 3.0.
class Model
attr_reader :attributes
def initialize(attributes)
@attributes = attributes
end
def ==(other)
Model === other and @attributes == other.attributes
end
end
describe "mocking" do
let(:model) { Model.new(id: 1, username: "jcoglan") }
it "literally matches" do
should_receive(:puts).with(Model.new(id: 1, username: "jcoglan"))
puts model
end
it "matches with a type mathcer" do
should_receive(:puts).with(Model.new(id: 1, username: instance_of(String)))
puts model
end
it "matches with a hash matcher" do
should_receive(:puts).with(Model.new(hash_including(id: 1)))
puts model
end
it "matches with composed hash and type matchers" do
should_receive(:puts).with(Model.new(hash_including(username: instance_of(String))))
puts model
end
end
__END__
Failures:
1) mocking matches with a type mathcer
Failure/Error: puts model
#<RSpec::ExampleGroups::Mocking:0x007f9eb15da758> received :puts with unexpected arguments
expected: (#<Model:0x007f9eb15d9560 @attributes={:id=>1, :username=>#<RSpec::Mocks::ArgumentMatchers::InstanceOf:0x007f9eb15d95b0 @klass=String>}>)
got: (#<Model:0x007f9eb15d93a8 @attributes={:id=>1, :username=>"jcoglan"}>)
# ./foo_spec.rb:23:in `block (2 levels) in <top (required)>'
2) mocking matches with a hash matcher
Failure/Error: puts model
#<RSpec::ExampleGroups::Mocking:0x007f9eb15d32f0> received :puts with unexpected arguments
expected: (#<Model:0x007f9eb15d2058 @attributes=#<RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher:0x007f9eb15d2080 @expected={:id=>1}>>)
got: (#<Model:0x007f9eb15d1f40 @attributes={:id=>1, :username=>"jcoglan"}>)
# ./foo_spec.rb:28:in `block (2 levels) in <top (required)>'
3) mocking matches with composed hash and type matchers
Failure/Error: puts model
#<RSpec::ExampleGroups::Mocking:0x007f9eb15d0a28> received :puts with unexpected arguments
expected: (#<Model:0x007f9eb15cbb40 @attributes=#<RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher:0x007f9eb15cbb68 @expected={:username=>#<RSpec::Mocks::ArgumentMatchers::Instance
Of:0x007f9eb15cbc08 @klass=String>}>>)
got: (#<Model:0x007f9eb15cba28 @attributes={:id=>1, :username=>"jcoglan"}>)
# ./foo_spec.rb:33:in `block (2 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment