Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

ruby on rails - How to avoid deprecation warning for stub_chain in RSpec 3.0?

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 symbols representing methods" do
    it "returns the correct value" do
      subject.stub_chain(:one, :two, :three).and_return(:four)
      expect(subject.one.two.three).to eq(:four)
    end
  end
end

Deprecation Warnings: Using stub_chain from rspec-mocks' old :should syntax without explicitly enabling the syntax is deprecated. Use the new :expect syntax or explicitly enable :should instead.

How this warning can be avoided?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...