2011-08-24 7 views
6

アクティブなサポート通知の仕様を知っている人はいますか?以下は動作しないようです。それはデフォルトのレールフレームワークの通知を検出しますが、カスタムのものは検出しません。rspecによるActiveSupport :: Notificationsのテスト

it 'sends a "product.search" notification to any subscribers listening' 
    ActiveSupport::Notifications.should_receive(:instrument).with("product.search", :search => search) 
    get :search, ... 
end 

Iは(DBのレコードを作成する際に、例えば、レコードカウント変化する)加入者のコードの結果を確認するために仕様を変更する場合には、通過します。それはそれがokを働いていることを確認します。しかし、加入者がここで何をしているのかが間違っているようだが、通知が送信されているという仕様にしたい。いかなる考えも認められるだろう。

編集:ここでは

は、私が仕様にしようとしているコントローラのコードです:

ActiveSupport::Notifications.instrument("product.search", :search => 'test') 
+0

あなたのタグは 'rpsec'ですか? [意図した 'rspec'?] – Shad

答えて

2

私は何かが何らかの形で干渉するべきで、定期的な:getアクションを使用してテストを達成することができませんでしたし、 start_processingprocess_action通知のみがトリガされます。私はそれがパフォーマンスのために無効になっていると思う。

しかし、これが正常に動作します:

it 'sends a "product.search" notification to any subscribers listening' 
    ActiveSupport::Notifications.should_receive(:instrument).with("product.search", :search => "test") 
    controller.index 
end 
5

を、私は同じ問題を持っていたおよび以下のRSpecのヘルパーメソッドを書いた:この道を

def notification_payload_for(notification) 
    payload = nil 
    subscription = ActiveSupport::Notifications.subscribe notification do |name, start, finish, id, _payload| 
     payload = _payload 
    end 

    yield 

    ActiveSupport::Notifications.unsubscribe(subscription) 

    return payload 
    end 

を次のように、私はそれを使用することができます。

it "should raise the my_notification_name notification" do 
    payload = notification_payload_for('my_notification_name') do 
     # do stuff that should raise the proper notification 
    end 

    # test to see that the payload has the correct info 
    end 
関連する問題