0

ディレクトリの下に多数のヘルパー クラスが保存されており、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_instancestubは?!

4

3 に答える 3

0

allow_any_instance_ofhttp://www.rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods:allow_any_instance_ofexpectに従って、構文が有効になっている場合にのみ有効になります。おそらく、古い構文でのみ動作するように RSpec を構成しました。should

于 2014-10-20T13:47:52.963 に答える
0

私もこの問題に遭遇しました。before :allひょっとしてブロックから呼び出していませんか?に移動した後に機能したため、明らかにこれは許可されていませんbefore :each

それが役立つことを願っています!

于 2015-01-21T19:06:44.497 に答える