Rspec-rails: Can't set host in routing specs

Created on 30 Jul 2010  路  8Comments  路  Source: rspec/rspec-rails

Hi, I'm trying to set a different host using a subdomain (test.example.com).
The route I'm trying to test is the following (rails 3 rc1):

match 'profile', :to => 'users#profile', :constraints => {:subdomain => /.+/ }

I've searched everywhere, but the host! method that everybody mentions doesn't seem to exist anymore, and @request is nil. No answers on rspec-users as well.
Is this feature missing or I'm simply doing it in the wrong way ?

Most helpful comment

FYI - the solution is now to pass the full URL:

{ :get => "http://test.example.com" }.should route_to(:controller => :users, :action => :profile, :subdomain => 'test')

All 8 comments

Is this a problem with a spec? If so, please show me the spec and the full backtrace of the failure. Thx.

Yes, it's a problem with a spec, the problem is related to the fact that I'm not able to set the host.

describe "routing to profiles" do

it "routes /profile to UsersController#profile for slug" do
# here I should set the host test.example.com
# if I uncomment @request.host I have undefined method `host=' for nil:NilClass
# @request.host = 'test.example.com'
{ :get => "profile" }.should route_to(
:controller => :users, :action => :profile, :subdomain => 'test' )
end

end

Without setting the host the error is

1) routing to profiles routes /profile to UsersController#profile for slug
Failure/Error: { :get => "profile" }.should route_to(
undefined method `subdomain' for #Rack::Request:0x102ed9268
# ./spec/routing/users_routing_spec.rb:7

The route_to matcher delegates to Rails assert_routing, which was not only not updated to support specifying constraints, it doesn't even work at all if you have this sort of constraint.

I've raised the issue in the rails tracker: https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/5260. I'll leave this one open for now, as that's what I've done for the other issue that needs a fix in rails.

I'm also having the same trouble. Can't believe it didn't check here first! Looks like I've just gone through the same set of attempts as ngw.

I'd like to highlight that @ngw posted

# if I uncomment @request.host I have undefined method `host=' for nil:NilClass
# @request.host = 'test.example.com'

I am also having this problem while upgrading an application from rails 2.3.x to 3.0rc. I was setting controller.request.host in a before block. That errors with an undefined method for request as does the method posted by @ngw.

Any thoughts? How should I be setting the domain for each controller spec?

This has been fixed in rails and will be part of the 3.0.0.rc.2 release.

See https://rails.lighthouseapp.com/projects/8994/tickets/5005

FYI - the solution is now to pass the full URL:

{ :get => "http://test.example.com" }.should route_to(:controller => :users, :action => :profile, :subdomain => 'test')

It is success but if we run all specs failing these examples
Example:
In my case
rspec spec/routing/individual_routing_spec.rb
4 examples ,0 fails
When I run all specs rspec spec/
17 examples,4 fails

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Nerian picture Nerian  路  37Comments

beydogan picture beydogan  路  34Comments

ledbettj picture ledbettj  路  42Comments

bronson picture bronson  路  24Comments

RemyMaucourt picture RemyMaucourt  路  35Comments