我的spec/support
目录下存储了许多帮助程序类,我在许多测试中重复使用它们。例如,foo_helper.rb
class FooHelper
def self.stub_thing
Foo.any_instance.stub(:thing)
Foo.any_instance.stub(:thing=)
end
end
Foo
在许多测试中使用,所以我只会require ../spec/support/foo_helper.rb
在我希望能够使用的每个规范中使用FooHelper.stub_thing
. 这一切在 RSpec 2.x 中运行良好
升级到 RSpec 3.1 后,我看到以下折旧警告:
Using `any_instance` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from app/spec/support/foo_helper.rb:4:in `stub_thing'.
因此,添加rspec-actvemodel-mocks
到我的 Gemfile 中:
gem 'rspec-rails', '~> 3.1'
group :development, :test do
gem 'rspec-activemodel-mocks', '~> 1.0'
end
并按照文档,我将代码更改为:
class FooHelper
def self.stub_thing
allow_any_instance_of(Foo).to receive(:thing)
allow_any_instance_of(Foo).to receive(:thing=)
end
end
然后导致我的测试失败并出现以下错误:
Failure/Error: FooHelper.stub_thing
NoMethodError:
undefined method `allow_any_instance_of' for FooHelper:Class
# ./spec/support/foo_helper.rb:4:in `stub_amount'
# ./spec/models/parent_model.rb:33:in `block (2 levels) in <top (required)>'
怎么allow_any_instance_of
不能定义,when any_instance
and stub
are?!