2011-09-29 68 views
1

我正在使用Ruby on Rails 3.1.0,rspec-rails 2DelajdJob寶石。爲了測試用戶註冊時是否發送了電子郵件,我正在嘗試測試延遲的電子郵件,但我遇到了一些麻煩\疑問(與DelayedJob寶石沒有直接關係)。我應該如何測試郵件文件中的類對象?

在我的控制,我有:

def create 
    ... 
    Users::Mailer.delay.sign_up(@user) 
    ... 
end 

在我的郵件文件我有:

class Users::Mailer 
    def sign_up(user) 
    @user = user 

    # Note: 'authentication' refers to an associated model 
    @authentication = user.authentication.user_token 
    ... 
    end 
end 

在我的規格文件我有:

describe "POST create" do 
    it "sends e-mail" do 
    post :create, { :email => '[email protected]' } 

    # Here I would like to test 
    # (1) if the e-mail is send 
    # (2) if the e-mail is send to the '[email protected]' address 
    end 
end 

如果我運行規範我收到以下錯誤:

Failure/Error: post :create, 
NoMethodError: 
    undefined method `user_token' for nil:NilClass 

我該如何解決並測試電子郵件是否已發送?我應該嘲笑還是存根用戶類對象實例?你對我的建議有什麼建議?

回答

0
def create 
    ... # whatever is going on here is not setting @user.authentication 
    Users::Mailer.delay.sign_up(@user) 
    ... 
end 

編輯:我意識到我回答了您遇到的錯誤的問題。至於嘲笑用戶對象,我會傾向於在這種情況下使用模擬。在這種情況下測試的重要之處在於發送了一封電子郵件,併發送至[email protected]。我會發布你需要創建一個用戶然後驗證。

+0

你是什麼意思? – user502052

+0

這個錯誤「未定義的方法'user_token'爲零:NilClass」意味着@ user.authentication未被設置,該代碼在某處... –

+0

我知道...但我也要求提供建議那個「一般」問題\情景... – user502052

相關問題