ディレクトリの下に多数のヘルパー クラスが保存されており、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
ように定義することはできません, いつany_instance
とstub
は?!