Skip to content

Instantly share code, notes, and snippets.

@malpinder
Last active August 29, 2015 14:21
Show Gist options
  • Save malpinder/d50a9985a74e53743303 to your computer and use it in GitHub Desktop.
Save malpinder/d50a9985a74e53743303 to your computer and use it in GitHub Desktop.
Hopelessly egocentric objects that make Rspec mock expectations explode.
require 'spec_helper'
# https://github.com/raganwald-deprecated/homoiconic/blob/master/2009-02-02/hopeless_egocentricity.md
class HopelesslyEgocentric < BasicObject
def method_missing(*args, &block)
self
end
# These methods are required to make the tests output their failures normally.
#def to_str
# 'HopelesslyEgocentric'
#end
#def to_ary
# [self]
#end
#def to_int
# 0
#end
end
describe 'A test case for hopelessly egocentric objects' do
let(:string) { 'a string' }
let(:hopelessly_egocentric_object) { HopelesslyEgocentric.new }
it 'can be used in successful expectations' do
expect(string).to receive(:include?).with(hopelessly_egocentric_object)
string.include? hopelessly_egocentric_object
end
it 'cannot be used as an argument in failing expectations' do
expect(string).to receive(:include?).with(hopelessly_egocentric_object)
end
it 'cannot be involved in failing expectations' do
expect(string).to receive(:include?).with(nil)
string.include?(hopelessly_egocentric_object)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment