views:

1086

answers:

4

Hello,

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)

A: 

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.

Jamie Flournoy
Yes, I changed that already to: ENV["RAILS_ENV"] ||= "test"
Lichtamberg
+1  A: 

Use :host option in your url.

invitation_activation_url(1, :host => "www.xyz.at")
  => "www.xyz.at/signup/1231hj23jh23"
edbond
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!)
Lichtamberg
A: 

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

webmat
Can it be - that this is a webrat bug?
Lichtamberg
A: 

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.

Henrik N