How to avoid deprecation warning for stub_chain in RSpec 3.0?

后端 未结 4 1829
清歌不尽
清歌不尽 2020-12-13 09:26

When I run a test with stub_chain, I\'ll get a deprecation warning.

describe \"stubbing a chain of methods\" do
  subject { Object.new }

  context \"given s         


        
相关标签:
4条回答
  • 2020-12-13 09:50

    In order to get rid of the warning with your code as-is, you'll have to explicitly enable the should syntax in your config:

    RSpec.configure do |config|
      config.expect_with :rspec do |c|
        c.syntax = [:should, :expect]
      end
    end
    

    The replacement syntax for stub_chain is:

    allow(object).to receive_message_chain(:one, :two, :three).and_return(:four)
    expect(object.one.two.three).to eq(:four)
    

    More information about this and its usage in:

    • The pull request in rspec-mocks
    • The feature examples in rspec-mocks

    As of this writing, the change to receive_message_chain will be included in the 3.0.0.beta2 release of rspec-mocks (see the Changelog). If you want it right now, you'll have to live on the bleeding edge and add the specific commit reference in your Gemfile to get receive_message_chain working:

    gem 'rspec-mocks', github: 'rspec/rspec-mocks', ref: '4662eb0'
    

    Unfortunately, that doesn't actually answer your question about getting rid of the deprecation message, which I was unable to do, even with the pre-release version of rspec-mocks and
    c.syntax = [:should, :expect] set explicitly in my RSpec config.

    So, I would say your options are to either wait until 3.0.0.beta2 is released and see if the deprecation notices get fixed with your existing code at that time, or bring in the very latest changes and change your syntax to receive_message_chain.

    See Myron's answer for the actual solution.

    0 讨论(0)
  • 2020-12-13 09:55

    Here's a solution that worked for me - I'm using Rails 4.1.7 :

    Inside spec/spec_helpber.rb, set rspec-expectations’ and/or rspec-mocks’ syntax as following:

    RSpec.configure do |config|
      config.mock_with :rspec do |mocks|
        mocks.syntax = [:should, :expect]
      end
      config.expect_with :rspec do |expectations|
        expectations.syntax = [:should, :expect]
      end
    end
    

    Hope this helps someone else :)

    0 讨论(0)
  • 2020-12-13 09:57
    RSpec.configure do |config|
      config.mock_with :rspec do |c|
        c.syntax = [:should, :expect]
      end
    end
    

    Notice that it's setting the rspec-mocks syntax, not the rspec-expectations syntax, as Paul's answer shows.

    0 讨论(0)
  • 2020-12-13 10:00

    For anybody who wants to upgrade an old project to the new syntax, there is a tool here.

    As mentioned in the Relish blog, they will probably move the should syntax into an external gem in the future, this leads me to believe that it will eventually be made obsolete.

    0 讨论(0)
提交回复
热议问题