Skip to content

Instantly share code, notes, and snippets.

@thefotios
Last active July 31, 2020 16:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Allow instance_double's to pass class equivalence checks (see https://github.com/rspec/rspec-mocks/issues/794)
module VerifiedDoubleExtensions
CLASS_EQUIVALENCE_FUNCTIONS = %i[is_a? kind_of? instance_of?].freeze
def verified_double(klass, *args)
instance_double(klass, *args).tap do |dbl|
CLASS_EQUIVALENCE_FUNCTIONS.each do |fn|
allow(dbl).to receive(fn) do |*fn_args|
klass.allocate.send(fn, *fn_args)
end
end
allow(klass).to receive(:===).and_call_original # do the normal thing by default
allow(klass).to receive(:===).with(dbl).and_return true
end
end
end
RSpec.configure do |c|
c.include VerifiedDoubleExtensions
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment