Question

In my routes file I have:

resources :subscription, :only => [:show], :constraints => {:protocol => "https"}

I'm trying to add a spec for this route like this:

it "recognizes and generates #show" do
  { :get => "/subscription", :protocol => 'https' }.should route_to(:controller => "subscriptions", :action => "show")
end

However, the spec still fails. If I remove the :protocol => 'https', the spec also fails:

ActionController::RoutingError:    
  No route matches "/subscription" 
Was it helpful?

Solution

The (undocumented?) solution is to simply include an entire dummy url, like so:

it "recognizes and generates #show" do
  { :get => "https://test.host/subscription" }.should route_to(:controller => "subscriptions", :action => "show")
end

I figured it out from this ticket and this changeset.

OTHER TIPS

I'm not sure, but I think routes should be declared as plural — see Rails Routing from the Outside In. So it would be

resources :subscriptions

and in the spec

{:get => '/subscriptions', :protocol => 'https'}

Try if that passes without :protocol. If it does, skip the spec with HTTPS. That should not be tested on a unit-test level, but in an integration test.

Does it pass if you change to spec to this?:

{:get => '/subscription'}.should_not route_to(:controller => …)

That would at least give you the confidence that HTTP is not routed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top