Konrad
Konrad

Reputation: 1635

Rails, test mailer url

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?

Upvotes: 3

Views: 1898

Answers (4)

Chris Aitchison
Chris Aitchison

Reputation: 4654

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}"

Upvotes: 2

kilaulena
kilaulena

Reputation: 66

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' }

Upvotes: 5

iafonov
iafonov

Reputation: 5192

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:)

Upvotes: 0

loosecannon
loosecannon

Reputation: 7803

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

Upvotes: 0

Related Questions