2011-08-24 86 views
6

有誰知道如何規定主動支持通知?以下似乎並不奏效。它檢測到默認的rails框架通知,但不是我的自定義框架通知。使用rspec測試ActiveSupport ::通知

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

如果我改變規格,檢查用戶的代碼它傳遞(創建數據庫記錄時,例如記錄數的變化)的結果。這證實它工作正常。但是,規定用戶在這裏做什麼似乎是錯誤的,我只想規定通知正在發送。任何想法將不勝感激。

編輯:

這裏是我試圖符合規範的控制器代碼:

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的helper方法:

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