rspec / rspec/rspec

mock_of? to check types on doubles (to avoid unnecessary coercions)

Open
#43 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
117
Forks
51
Avg merge
59m
Merged PRs (30d)
3

Description

I have a feature request similar to rspec/rspec-mocks#794 (instance_double not matching the same class it is mocking) but I have a more specific use-case and solving for it doesn't necessary require stubbing #===.

There are many Ruby gems that support type coercion, like:

I believe they all do a type check to avoid unnecessary coercions. In the case of Hashie, coercion is skipped if the value is already the target class:

  it 'skips unnecessary coercions' do
      subject.coerce_key :foo, Coercable

      object = Coercable.new('bar')
      instance[:foo] = object
      expect(instance[:foo]).to eq(object)
    end

However, if the value is an double of the target class than it will be coerced:

    it 'skips unnecessary coercions (RSpec instance_double)' do
      subject.coerce_key :foo, Coercable

      object = instance_double(Coercable)
      instance[:foo] = object
      expect(instance[:foo]).to eq(object)
    end
Failures:

  1) Hashie::Extensions::Coercion#coerce_key skips unnecessary coercions (RSpec instance_double)
     Failure/Error: expect(instance[:foo]).to eq(object)

       expected: #<RSpec::Mocks::InstanceVerifyingDouble:0x3fe5cdd89080 @name="Coercable (instance)">
            got: #<Coercable:0x007fcb9bb11de0 @coerced=true, @value="RSpec::Mocks::InstanceVerifyingDouble">

       (compared using ==)

       Diff:
       @@ -1,2 +1,4 @@
       -#<RSpec::Mocks::InstanceVerifyingDouble:0x3fe5cdd89080 @name="Coercable (instance)">
       +#<Coercable:0x007fcb9bb11de0
       + @coerced=true,
       + @value="RSpec::Mocks::InstanceVerifyingDouble">

I'd like to be able to avoid that coercion. Rather than stubbing #kind_of? or #=== something like #mock_of? that behaves similar to #kind_of? on the doubled class would be useful.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the RSpec instance_double behavior described in the examples, especially the comparison with kind_of? and ===, and review the linked Hashie coercion case. Done means doubles can be recognized as representing their doubled class so type-based coercion can be skipped without stubbing those methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.