fqxp
fqxp

Reputation: 7939

Named routes in ActionMailer tests

I am sending invitations via e-mail containing a confirmation link. In my functional test I want to check if the mail body contains the correct URL like this:

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

On the line starting with url =, the test gets an error saying undefined method 'new_user_url', while in an ActionController test, the same line works with no problem.

I tried inserting the lines:

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

inside the class definition, but to no avail. I also tried using url_for, but that doesn't work either ("undefined local variable or method '_routes'"). I guess I need some import here, but which?

Upvotes: 1

Views: 667

Answers (1)

clyfe
clyfe

Reputation: 23770

You can include the dynamically generated url helpers module, and then use any url helper defined by it like so:

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

Upvotes: 3

Related Questions