5

I specified the default_url_options in my environments/test.rb with

config.action_mailer.default_url_options = { :host => "www.xyu.at" }

This is quite ok and in my cucumber story, where i test registration of users, the user-activation link gets generated right

invitation_activation_url(1)
=> "www.xyu.at/signup/1231hj23jh23"

But when I try to follow the link provided in the email with following code in features/steps/user_steps.rb (using email-rspec from http://github.com/bmabey/email-spec/tree/master):

When /^I follow the invitation link$/ do
  When 'I follow "'+invitation_activation_url(1) + '" in the email'
end

Here the url gets created with the default-host:

invitation_activation_url(1)
=> "www.example.com/signup/1231hj23jh23"

Can anybody help me? I don't get what I'm doing wrong....

Thanks!

EDIT:

It seems to do with the method

current_url

but I dont know where it comes from..?

EDIT:

And I have the right environment specified in my features/support/env.rb

ENV["RAILS_ENV"] ||= "test"

EDIT:

My temporary solution is, what edbond said,

invitation_activation_url(1, :host => "www.xyz.at")
  => "www.xyz.at/signup/1231hj23jh23"

but I dont want to name the domain explicit this way (i specified it already in my environments/test.rb file - that way it wouldnt be dry)

5 Answers 5

1

Use :host option in your url.

invitation_activation_url(1, :host => "www.xyz.at")
  => "www.xyz.at/signup/1231hj23jh23"

EDIT:

You can parse email body and get link

  mail = YourMailer.deliveries.last
  email_html = Nokogiri::HTML mail.body.to_s
  approve_link = email_html.at_css("a")["href"]
2
  • 1
    Yes, thats my temporary solution. But I don't want to name the domain explicit this way (-> duplication, as I configured it environments/test.rb before!) Jul 23, 2009 at 9:22
  • As Lichtamberg says, this is a one-off workaround if you need a single call to a route helper to work. But this is not a general solution, and would be cumbersome and bad practice to try to implement it for an app of any complexity.
    – Raphael
    Mar 6, 2013 at 20:31
1

I know its years since this was posted.. but I had this issue and took me hours to decipher until I got it figured out.

You should use instead

When /^I follow the invitation link$/ do
  When 'I follow "'+invitation_activation_path(1) + '" in the email'
end

the _url generates the URL path; whilst the _path generates the URI path. web_steps.rb uses the URI to determine the current_url which it uses to work out the host.

from http://api.rubyonrails.org/classes/ActionDispatch/Routing.html

Routes can be named by passing an :as option, allowing for easy reference within your source as name_of_route_url for the full URL and name_of_route_path for the URI path.

from web_steps.rb

Then /^(?:|I )should be on (.+)$/ do |page_name|                                                                                                                                     |  #     end                                                                                                                                                                           
  current_path = URI.parse(current_url).path                                                                                                                                         |  #                                                                                                                                                                                   
  if current_path.respond_to? :should                                                                                                                                                |  #     collection do                                                                                                                                                                 
    current_path.should == path_to(page_name)                                                                                                                                        |  #       get 'sold'                                                                                                                                                                  
  else                                                                                                                                                                               |  #     end                                                                                                                                                                           
    assert_equal path_to(page_name), current_path                                                                                                                                    |  #   end                                                                                                                                                                             
  end                                                                                                                                                                                |
end 
0

You say that you edited config/environments/test.rb. Are you sure that your Cucumber features are actually executing in the 'test' environment?

I recently added Cucumber to a project I'm working on, and it seems to set itself up to use a 'cucumber' environment by default.

In features/support/env.rb in my project there is this:

ENV["RAILS_ENV"] ||= "cucumber"

So if your project is similar, then you will need to customize config/environments/cucumber.rb as well.

1
  • Yes, I changed that already to: ENV["RAILS_ENV"] ||= "test" Jul 17, 2009 at 7:19
0

I'm not terribly familiar with Cucumber, so I can't say with certainty where exactly you'll have to apply this fix. But the problem is that the default_url_options is not set in another place where you're trying to generate your url...

So my advice is to first find out in what context the faulty url is being generated. Before or after it, just output self.class. That's the class you'll have to monkey-patch. For the example, let's say 'ClassName' was printed out.

When you have that, in your config/environments/test.rb, just add the attribute accessor and then set it to what you want:

class ClassName
  cattr_accessor :default_url_options
  # or mattr_ if it's a module
end

and then set it the same way as your actionmailer

ClassName.default_url_options = { :host => "www.xyu.at" }

This whole process can be useful as well when you want to generate urls in models or in other esoteric places (then you'll also need to include ActionController::UrlWriter).

1
  • Can it be - that this is a webrat bug? Jul 27, 2009 at 8:45
0

One solution (based on info here) is to have a step definition like

Given /^the domain "(.+?)"$/ do |domain|
  host! domain
end

and use it like

Given the domain "www.foo.com"

in features.

With that, though, I ran into issues where redirects were not followed. I tried applying this patch but had no luck.

I ended up using a very simple workaround in Cucumber's env.rb file:

# There is a bug in internal_redirect? when used with explicit (non-example.com) domains.
# This is a simple workaround but may break if we start specing external redirects.
# https://webrat.lighthouseapp.com/projects/10503/tickets/140-current_url-should-be-fully-qualified
class Webrat::Session
  alias internal_redirect? redirect?
end

As mentioned in the comment, this may of course break with external redirects, but we have none.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.