1
我正在使用Ruby on Rails 3.1.0,rspec-rails 2
和DelajdJob
寶石。爲了測試用戶註冊時是否發送了電子郵件,我正在嘗試測試延遲的電子郵件,但我遇到了一些麻煩\疑問(與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
我該如何解決並測試電子郵件是否已發送?我應該嘲笑還是存根用戶類對象實例?你對我的建議有什麼建議?
你是什麼意思? – user502052
這個錯誤「未定義的方法'user_token'爲零:NilClass」意味着@ user.authentication未被設置,該代碼在某處... –
我知道...但我也要求提供建議那個「一般」問題\情景... – user502052