如何避免RSpec 3.0中stub_chain的弃用警告?

Mil*_*cel 34 rspec ruby-on-rails

当我使用stub_chain运行测试时,我会收到弃用警告.

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
Run Code Online (Sandbox Code Playgroud)

弃用警告:不推荐使用stub_chainrspec-mocks的旧:should语法而不显式启用语法.使用新:expect语法或显式启用:should.

如何避免这种警告?

Pau*_*nti 68

为了按原样删除代码警告,您必须should在配置中明确启用语法:

RSpec.configure do |config|
  config.expect_with :rspec do |c|
    c.syntax = [:should, :expect]
  end
end
Run Code Online (Sandbox Code Playgroud)

替换语法stub_chain是:

allow(object).to receive_message_chain(:one, :two, :three).and_return(:four)
expect(object.one.two.three).to eq(:four)
Run Code Online (Sandbox Code Playgroud)

有关此内容及其用法的更多信息:

在撰写本文时,更改receive_message_chain将包含在3.0.0.beta2rspec-mocks 的发布中(请参阅更改日志).如果你现在想要它,你将不得不生活在最前沿,并在你的Gemfile中添加特定的提交引用以使其receive_message_chain工作:

gem 'rspec-mocks', github: 'rspec/rspec-mocks', ref: '4662eb0'
Run Code Online (Sandbox Code Playgroud)

不幸的是,这实际上并没有回答你关于摆脱折旧消息的问题,这是我无法做到的,即使使用预发布版本的rspec-mocks并
c.syntax = [:should, :expect] 在我的RSpec配置中明确设置.

所以,我想说你的选择是等到3.0.0.beta2发布后再看看当时是否用现有代码修改了弃用通知,或者引入最新的更改并将语法更改为receive_message_chain.

请参阅Myron对实际解决方案的回答.

  • 你用`stub_chain`尝试过这个吗?即使使用`:should`显式启用,警告仍然保留在github上标记为`v3.0.0.beta1`的版本,我认为这是最新版本. (2认同)

Myr*_*ton 32

RSpec.configure do |config|
  config.mock_with :rspec do |c|
    c.syntax = [:should, :expect]
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意,正如Paul的回答所示,它正在设置rspec-mocks语法,而不是rspec-expectations语法.