2011-02-23 73 views
2

嘿大家我想讓Rspec測試一個動作是否調用了我從ActiveMailer繼承但是一直沒有運氣的模型中的特定方法。所以作爲一個快速樣機我有以下情況。型號UserNotifier:RSpec測試調用到ActiveMailer模型

class UserNotifier < ActionMailer::Base 
    def foobaz 
    end 
end 

控制器密碼控制器:

class PasswordsController < ApplicationController 

def foobar 
    UserNotifier.foobaz 
end 

而且規格:

describe "GET 'foobar'" do 

    it "should call the UserNotifier foobaz method" do 
    UserNotifier.should_receive(:foobaz) 
    get :foobar 
    end 
end 

,但我總是與這個失敗而告終:

1) PasswordsController GET 'foobar' should call the UserNotifier foobaz method 
Failure/Error: UserNotifier.should_receive(:foobaz) 
    (<UserNotifier (class)>).foobaz(any args) 
     expected: 1 time 
     received: 0 times 

人啓發我爲什麼RSpec不註冊UserNotifier.foobaz方法被調用?

+0

我重複了這一點,並通過示例。也許你應該在發現問題的地方發佈原始代碼。 – zetetic 2011-02-24 00:12:54

+0

這是原始代碼 - 我沒有改變一件事,也無法通過。 – 2011-02-24 02:24:32

+2

我認爲它沒有進入'PasswordsController',因爲你在那裏或在它的一個超級控制器中進行了某種登錄檢查。 – 2011-02-24 04:39:30

回答

0

在評論中由Ryan Bigg回答。作爲一個before_filter,導致該方法永遠不會運行

0

我有完全相同的問題,但它不是由於任何過濾器(不使用任何)。一個區別是,我在ApplicationController上調用一個動作,但我不明白爲什麼這很重要。 foob​​az方法肯定會被調用,因爲rspec運行包括我在那裏插入的輸出,但是Rspec沒有看到調用。任何其他的想法可能是錯誤的?

+0

不要緊;我嘗試了各種嘲諷,在整理這些信息時接聽電話,而他們失敗了;相當於UserNotifier.should_receive(:foobaz)現在可以使用 – fakeleft 2012-02-24 20:18:18