1

我有一个密码重置邮件,我希望能够在我的测试中执行以下操作:

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>

你如何像edit_password_reset_path(user.password_reset_token)在测试单元中那样访问 URL 助手?

我从 RSpec 切换过来,这从来都不是问题,助手刚刚工作,我会这样做:

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

1 回答 1

5

对于将来有兴趣解决此问题的任何人,您可以将其粘贴到您的测试文件中:

include Rails.application.routes.url_helpers

我不知道这是否是这样做的方法,我不确定它会给你的测试增加多少开销,但它工作正常。

于 2013-03-06T00:26:09.397 回答