【问题标题】:Rails mailer test not interpreting URL helpersRails 邮件程序测试不解释 URL 帮助程序
【发布时间】:2013-02-20 14:41:17
【问题描述】:

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

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

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

    include Rails.application.routes.url_helpers
    

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

    【讨论】:

    • 这在 rails 3.2 中效果很好,但在 rails 4 中它会导致“ActionView::Template::Error: 无法处理传递给 url_for 的参数。请要求路由或提供您自己的实现”错误more info
    猜你喜欢
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多