2017-07-20 6 views
0

RSpec.describeブロックにincludeのdというヘルパーモジュールがあります。そこにあるほとんどのメソッドは、ログイン/ログアウト状態の管理に関連しているので、そのヘルパーを使っているものが自動的にグローバル状態をクリーンアップして、ダブルス/モックを漏らさないようにしたいと考えています。rspecヘルパーモジュールに `after`ブロックを追加する

しかし、すべての例では、in the docsは、ちょうどヘルパーメソッドを追加しているようですが、余分なものはありません。before/afterブロックです。

afterブロックを追加して、ヘルパーモジュールの既存のブロックを上書きすることはできますか?

答えて

1

私はあなたの質問を正しく理解している場合、このような何かがトリックを行う必要があります。

helpers.rb

module Helpers 
    def help 
    :available 
    end 

    def self.included(base) 
    base.after(:each) do 
     puts "after in helpers.rb" 
    end 
    end 
end 

example.rb

require './helpers' 
RSpec.configure do |c| 
    c.include Helpers 
end 

RSpec.describe "an example group" do 
    after(:each) do 
    puts "After in example.rb" 
    end 

    it "has access to the helper methods defined in the module" do 
    expect(help).to be(:available) 
    end 
end 

、その後

それを実行している(例 afterブロックを使用して、リンク上の最初の1が追加されていることです。)
$ rspec example.rb 
# After in example.rb 
# after in helpers.rb 
# . 
# Finished in 0.00196 seconds (files took 0.07939 seconds to load) 
# 1 example, 0 failures 

私が質問を誤解した場合は、私に教えてください。私はもっと明確にするか、例を変更することができます。

1

答えははいです。テスト中に作成されたbefore | afterブロックは、ヘルパーファイルのbefore | afterブロックの前に実行されます。

だからあなたブロックヘルパーブロックを上書きしません。