3

My application sends E-mails containing absolute urls.

I set host in config/environment/development.rb

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

Now I want to test if Email contains valid url. Using regular expression I take out full url from E-mail and want to visit this address using Capybara function.

mail = ActionMailer::Base.deliveries.last
address = mail.body.to_s[%r{http.+/edit}]
visit address;

But I don't know what host should be set in config/environment/test.rb

When I set localhost:3000 it tries to connect to my local server started by rails server command.

Do you have any ideas to solve this problem?

Konrad
  • 1,605
  • 3
  • 24
  • 45

4 Answers4

5

What worked for me:

Capybara.server_port = 3005

(see https://github.com/vangberg/capybara/commit/5784f03d6aa87e63e759abda794a43738e4f320f)

and

config.action_mailer.default_url_options = { :host => 'localhost:3005' }
Rob W
  • 341,306
  • 83
  • 791
  • 678
kilaulena
  • 66
  • 1
  • 3
2

To enable this to work when hardcoding Capybara ports is not an option (eg. parallel specs), put this in your spec_helper.rb

 YourApp::Application.config.action_mailer.default_url_options[:host] = "localhost:#{Capybara.server_port}"
Chris Aitchison
  • 4,656
  • 1
  • 27
  • 43
0

Instead of visiting the URL why not just validate it against a RegEx, make sure that it is valid, encoded correctly, points to the right controller.

You probably don't want to actually interact with the server, and if you do not the production server so localhost:3000 is a goo option

loosecannon
  • 7,683
  • 3
  • 32
  • 43
  • Test environment is ready to handle this request and I want to visit this address. I think the only way is to convert URL to realtive path. Is there a good way to do this if address is String object? – Konrad Jul 21 '11 at 16:19
  • Use URI.parse(string) to get a URI object, then change it's host, then spit it back out as a string. – Philip Hallstrom Jul 21 '11 at 18:31
0

Hey here is scary code snippet that maybe could help you. I use email_spec to simplify working with email in specs and cucumber scenarios and it already has some helpers to simplify your task. In my application I have a little more complexer situation so I was forced to write my own parser. Here is the code. Enjoy:)

iafonov
  • 5,152
  • 1
  • 25
  • 21