【问题标题】:I get a deprecation warning when I use 'stub_chain' of gem 'rspec-mock'当我使用 gem 'rspec-mock' 的 'stub_chain' 时收到弃用警告
【发布时间】:2018-07-19 12:49:05
【问题描述】:

我正在使用 RSpec 和 rspec-mocks 为一些对象制作一个模拟。 我反对的是以下内容。

在规范文件中

describe 'foo' do
  before do
    Mock.start
  end
end

在模拟文件中

module Mock
  def self.start
    SomeClass.stub_chain(:foo).and_return(Mock.mock_create)
  end

  def self.mock_create
    return json
  end
end

但如果我使用stub_chain,则会出现以下弃用警告。

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.

您有解决此警告的任何想法吗? allow 方法看起来没用,因为我想要像 Object.something_instead_of_stub_chain(:create).and_return(Mock.mock_create) 这样的代码。

【问题讨论】:

    标签: rspec ruby-on-rails-5 rspec-mocks


    【解决方案1】:

    新的做法是

       expect(SomeClass).to receive_message_chain(:foo, :bar, :baz).and_return(something_here)
       # or if not a chain
       expect(SomeClass).to receive(:foo).and_return(something_here)
    

    您可以使用allow 代替expect。如果根本不调用该方法,则不会失败,但会在调用时返回指定的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      相关资源
      最近更新 更多