Skip to content

Instantly share code, notes, and snippets.

@thefotios
Last active July 31, 2020 16:04
Show Gist options
  • Save thefotios/23912e524f585d855606dbec713df388 to your computer and use it in GitHub Desktop.
Save thefotios/23912e524f585d855606dbec713df388 to your computer and use it in GitHub Desktop.
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