You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was running into a problem with rspec's redirect_to matcher in my controller specs. If a controller was redirecting to a url that only exists in a subdomain, redirect_to would fail because it ignores the host part of the 302 response. I saw this with rails 2.3.8, rspec-rails 1.3.2 and subdomain-fu 0.5.4. Here's an example, followed by a workaround.
# routes
map.with_options :conditions => {:subdomain => 'mobile'} do |mobile|
mobile.resources :phone_numbers
end
# a controller
class PhoneNumbersController < ActionController::Base
def new; end
def edit; redirect_to new_phone_number_url; end
end
# and a spec
describe '#edit' do
it "should redirect" do
request.host = 'mobile.test.host'
get 'edit'
puts response.body # prints the correct url of http://mobile.test.host/phone_numbers/new
response.should redirect_to(edit_phone_number_url) # this raises ActionController::RoutingError
end
end
I traced the problem to the rspec-rails file lib/spec/rails/matchers/redirect_to.rb where the url from the 302 response gets converted into a route hash. But it ignores the host part of the url so the hash wouldn't have :subdomain => 'mobile'. This would cause the route not to be found, raising the error. I monkey patched rspec-rails like this which solved my problem:
module Spec
module Rails
module Matchers
class RedirectTo #:nodoc:
# a monkey patch to make rspec aware of subdomains when using the redirect_to matcher
def path_hash(url)
uri = URI.parse(url)
ActionController::Routing::Routes.recognize_path uri.path, {
:method => :get,
:subdomain => SubdomainFu.subdomain_from(uri.host),
}
end
end
end
end
end
I'm not sure whether subdomain-fu should try to patch rspec like above, or if rspec could be doing something better to avoid the issue. It seems like rspec is taking a shortcut converting the url to a hash which bypasses the route extensions applied by subdomain-fu.
The text was updated successfully, but these errors were encountered:
I found another issue. When passing hash arguments to the redirect_to matcher it will always fail if the redirect url host is different than the request host. My workaround is to pass a string argument instead.
did this ever get fixed? in my app i'm redirecting the host for certain request.paths and i'm finding the same issue -- redirect_to(...) is automatically assuming the host and i can't test against it.
I was running into a problem with rspec's redirect_to matcher in my controller specs. If a controller was redirecting to a url that only exists in a subdomain, redirect_to would fail because it ignores the host part of the 302 response. I saw this with rails 2.3.8, rspec-rails 1.3.2 and subdomain-fu 0.5.4. Here's an example, followed by a workaround.
I traced the problem to the rspec-rails file lib/spec/rails/matchers/redirect_to.rb where the url from the 302 response gets converted into a route hash. But it ignores the host part of the url so the hash wouldn't have :subdomain => 'mobile'. This would cause the route not to be found, raising the error. I monkey patched rspec-rails like this which solved my problem:
I'm not sure whether subdomain-fu should try to patch rspec like above, or if rspec could be doing something better to avoid the issue. It seems like rspec is taking a shortcut converting the url to a hash which bypasses the route extensions applied by subdomain-fu.
The text was updated successfully, but these errors were encountered: