2011-11-18 63 views
1

我通过电子邮件发送包含确认链接的邀请。在我的功能测试中,我想检查邮件正文中包含正确的URL是这样的:ActionMailer测试中的命名路线

class InvitationSenderTest < ActionMailer::TestCase 
    test "invite" do 
    ... 
    url = new_user_url(:www_host => 'localhost', :confirm_key => invitation.confirm_key) 
    assert_match /http:\/\/localhost#{url}/, mail.body.encoded 
    end 
end 

在开始url =线,测试得到一个错误说undefined method 'new_user_url',而在ActionController的测试,在同一行工作没有问题。

我试图插入线:

include ActionDispatch::Routing 
include ActionView::Helpers::UrlHelper 

类的定义里面,但无济于事。我也尝试使用url_for,但这也不起作用(“未定义的局部变量或方法'_routes'”)。我想我需要一些导入,但哪些?

回答

3

可以包括动态生成的网址助手模块,然后使用由它像这样定义的URL帮手:

class InvitationSenderTest < ActionMailer::TestCase 
    include Rails.application.routes.url_helpers 

    test "invite" do 
    ... 
    url = new_user_url(:www_host => 'localhost', :confirm_key => invitation.confirm_key) 
    assert_match /http:\/\/localhost#{url}/, mail.body.encoded 
    end 
end