简体   繁体   English

Rails邮件测试不解释URL助手

[英]Rails mailer test not interpreting URL helpers

I've got a password resets mailer and I'd like to be able to do the following in my tests: 我有一个密码重置邮件程序,我希望能够在测试中执行以下操作:

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

But it blows up because of the URL helper, so I have to do this instead to make my test run: 但由于使用了URL辅助程序,它炸毁了,所以我必须这样做来进行测试:

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

Which works fine, but don't look, or feel, so good. 哪个效果很好,但看起来或感觉都不太好。

It seems that ActionMailer::TestCase doesn't know about the path because the following fails: 似乎ActionMailer :: TestCase不知道路径,因为以下操作失败:

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

How do you access URL helpers like edit_password_reset_path(user.password_reset_token) in test-unit? 您如何在测试单元中访问URL帮助程序,例如edit_password_reset_path(user.password_reset_token)

I'm switching over from RSpec where this was never an issue, the helpers just worked and I'd do this: 我要从RSpec切换到那里,这从来都不是问题,助手只是工作了,我会这样做:

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

For anyone who is interested in solving this in the future, you can stick this in your test file: 对于将来有兴趣解决此问题的任何人,您都可以将其粘贴在测试文件中:

include Rails.application.routes.url_helpers

I don't know if this is thee way to do this, and I'm not sure how much overhead it adds to your tests, but it works ok. 我不知道这是否是执行此操作的方法,我不确定它会给您的测试增加多少开销,但是效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM