2016-08-04 7 views
1

私はapplication controllerにメソッドを持ち、インテグレーションの仕様ではどこでもそれを で使いたいと思っています。Rspecはアプリケーションコントローラメソッドを使用します

私は現在、私は

allow_any_instance_of(ApplicationController).to receive(:set_seo).and_return('seo_text') 

を使用するすべてのスペック

でそれにメソッドを追加する必要はありませんが、それは不便です。

どうすればいいですか?あなたが設定することができ、あなたのRSpecのコンフィグで

答えて

1

前とブロックの後:

スイート前

する前に、すべての

する前に、すべての後の各

後の各

そのためにはスイート

https://www.relishapp.com/rspec/rspec-core/v/2-2/docs/hooks/before-and-after-hooks

後に

私は示唆している:

RSpec.configure do |config| 
    config.before(:suite) do 
    allow_any_instance_of(ApplicationController).to receive(:set_seo).and_return('seo_text') 
    end 
end 

編集:

それは問題を引き起こす可能性がbefore(:suite)ことが表示されます。あなたはbefore(:each)

+0

'before(:each)'が正常に動作します。 – user

0

を使用するためにそれが動作しない場合は

私はspec_helper_integrationファイルを作成し、そこに統合仕様に固有の機能を入れてしまうでしょう。

すべてのスペックの上部にはすでにrequire 'rails_helper'があるはずです。お使いの統合スペックの一番上に置く:

require 'rails_helper' 
require 'spec_helper_integration' 

は、その後、あなたのrails_helper.rbファイルと同じフォルダにspec_helper_integration.rbファイルを作成します。

spec_helper_integration:

#I'm taking a guesstimate as to your integration spec configuration, but it's 
#likely something like the following line: 

#don't also have this in your spec_helper or rails_helper files: 
require 'capybara/rails' 

#configure your integration specs: 
RSpec.configure do |config| 
    config.before(:each) do 
    allow_any_instance_of(ApplicationController).to receive(:set_seo).and_return('seo_text') 
    end 
end 

それは、それが唯一必要とする場所にコードを分離することをお勧めです。このようにすると、ApplicationControllerメソッドのスタブは、統合仕様の実行中にのみ有効になり、他の仕様(ユニットやコントローラの仕様など)では有効になりません。

今後の統合仕様固有のコードは、spec_helper_integrationファイルにのみ入れてください。

関連する問題