ruby-on-rails - 升级到 RSpec 3 并遵循弃用通知会导致 "undefined method ' 允许'"

标签 ruby-on-rails ruby rspec cucumber rspec3

在升级 RSpec 之前,我的 features/support/hooks.rb 文件中有这个 block :

After do
  begin
    Challenge.unstub(:current)
  rescue RSpec::Mocks::MockExpectationError
  end
end

升级后,我收到此通知:

DEPRECATION: Using unstub from rspec-mocks' old :should syntax without explicitly enabling the syntax is deprecated. Use allow(...).to_receive(...).and_call_original or explicitly enable :should instead. Called from /Users/grant/xx/features/support/hooks.rb:37:in block in <top (required)>.

好吧,听起来很简单。我将代码更改为:

After do
  begin
    allow(Challenge).to receive(:current).and_call_original
  rescue RSpec::Mocks::MockExpectationError
  end
end

但现在我得到:

undefined method allow for #<Cucumber::Rails::World:0x007facbed9f1d0> (NoMethodError)

什么?来吧 RSpec,我完全按照你告诉我的去做了!

根据一些谷歌搜索,我尝试添加 require 'rspec/expectations'到该文件的顶部。它没有做任何事情。

任何人都可以补充我所缺少的内容吗?

最佳答案

也许您的 RSpec 配置未启用 allow 语法。在您的 RSpec 配置文件中,可能是 spec/spec_helper.rbspec/rails_helper.rb,您是否有类似以下内容的内容?

RSpec.configure do |config|
  config.mock_with :rspec do |c|
    c.syntax = :should
  end
end

如果有,看看是否改成

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

解决了您的问题。然后将 should 的所有使用替换为 allow 的使用,并升级 :should 语法的其他使用,可能使用 transpec ,并禁用 :should 语法,您就可以了解最新情况。

来源:https://relishapp.com/rspec/rspec-expectations/docs/syntax-configuration

关于ruby-on-rails - 升级到 RSpec 3 并遵循弃用通知会导致 "undefined method ' 允许'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31622385/

相关文章:

C# 的 LINQ 用于在 ruby​​ 中等效的集合操作

ruby-on-rails - 通过 includes 从 id 数组中选择

ruby-on-rails - 使用 RSpc 2.9.0 + FactoryGirl 3.2 通过验证测试 has_many

ruby-on-rails - Rails 合并两个哈希数组?

html - 在索引页上显示另外两个不同表的数据

ruby-on-rails - 检索我的 Adsense 报告作为服务器应用程序

ruby - 未返回 stub 方法的返回值

ruby-on-rails - 如何在cucumber中看到页面写入 "should not"?

mysql - Rails 上模型中与字符串 id 字段的多对多关系出现 NameError

ruby-on-rails - 使用 ActiveRecord 从对象到表