在升级 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. Useallow(...).to_receive(...).and_call_original
or explicitly enable:should
instead. Called from /Users/grant/xx/features/support/hooks.rb:37:inblock 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.rb
或 spec/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/