2013-03-06 37 views
1

我有一個密碼重置郵件,我希望能夠做到在我的測試如下:Rails的郵件測試不解釋URL傭工

assert_match edit_password_reset_path(user.password_reset_token), mail.body.encoded 
# undefined method `edit_password_reset_path' for #<UserMailerTest:0xb6c2ea0> 

但它的衝擊,因爲URL幫手了,所以我必須這樣做,而不是讓我的測試運行:

assert_match "\/password_resets\/#{user.password_reset_token}\/edit", mail.body.encoded 

哪些工作正常,但不看,或感覺,那麼好。

看來的ActionMailer :: TestCase的不知道的路徑,因爲下面的失敗:

assert_match "#{edit_password_reset_path(user.password_reset_token)}", mail.body.encoded 
# undefined method `edit_password_reset_path' for #<UserMailerTest:0xb6c2ec8> 

如何訪問URL傭工像edit_password_reset_path(user.password_reset_token)測試單元?

我從RSpec的切換,其中,這是從來沒有一個問題,助手只是工作,我應該這樣做:

it "sends the user a password reset url" do 
    mail.body.encoded.should match(edit_password_reset_url(user.password_reset_token)) 
end 

回答

5

對於任何人誰是有興趣在將來解決這種情況,可以在您的測試文件中堅持這一點:

include Rails.application.routes.url_helpers 

我不知道這是否是做到這一點你方式,我不知道它有多大的開銷會增加你的測試,但它工作正常。

+0

這在軌道3.2很好,但在軌道4它導致「ActionView :: Template :: Error:參數傳遞給url_for無法處理。請求路線或提供您自己的實現」錯誤[更多信息] (http://stackoverflow.com/questions/20660604/using-the-correct-url-for-method-in-a-rails-engine-spec) – tekniklr 2015-04-09 19:56:51