Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix RSpec/MessageChain "better" docs. #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2019

Conversation

schwern
Copy link
Contributor

@schwern schwern commented Dec 12, 2019

The original code results in an error because receive takes a single argument of the method name which it tries to symbolize.

     NoMethodError:
       undefined method `to_sym' for {:bar=>#<Thing:0x00007ff34792c178 @baz=42>}:Hash
       Did you mean?  to_s
                      to_set

I believe what it's trying to do is allow(foo).to receive(:bar).and_return(thing).


Before submitting the PR make sure the following are checked:

  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • N/A Added tests.
  • N/A Added an entry to the changelog if the new code introduces user-observable changes.
  • The build (bundle exec rake) passes (be sure to run this locally, since it may produce updated documentation that you will need to commit).

The original code results in an error, `receive` takes a single argument of the method name.

```
     NoMethodError:
       undefined method `to_sym' for {:bar=>#<Thing:0x00007ff34792c178 @baz=42>}:Hash
       Did you mean?  to_s
                      to_set
```
@schwern
Copy link
Contributor Author

schwern commented Dec 12, 2019

Here's some code showing the equivalency between the bad and better.

require 'rspec'

class Thing
  attr_accessor :baz
  
  def initialize(baz:)
    @baz = baz
  end
end

class Foo
  def bar
    23
  end
end

RSpec.describe 'receive' do
  let(:foo) { Foo.new }
  
  shared_examples 'baz is 42' do
    it 'foo.bar.baz is 42' do
      expect(foo.bar.baz).to eq 42
    end
  end
  
  context 'with receive_message_chain' do
    before do
      allow(foo).to receive_message_chain(:bar, :baz).and_return(42)
    end

    it_behaves_like 'baz is 42'
  end
  
  context 'with receive.and_return' do
    let(:thing) {
      Thing.new(baz: 42)
    }
    before do
      allow(foo).to receive(:bar).and_return(thing)
    end
    
    it_behaves_like 'baz is 42'
  end
end

Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. Thank you!

@Darhazer Darhazer merged commit 28131fe into rubocop:master Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants