Last active
July 31, 2020 16:04
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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