cih
cih

Reputation: 1980

RSpec Routing Error in test but not in app

I have a route in a app I'm working on, it looks like this...

  match ":place_id/:id", :controller => "companies", :action => "show", :place_id => /\S{2}/

So in my browser I can navigate to this URL.

mylocalmachine/gb/some-company

In the server log its all good.

Started GET "/gb/some-company" for 127.0.0.1 at 2013-04-30 17:44:52 +0100
Processing by CompaniesController#show as HTML
  Parameters: {"place_id"=>"gb", "id"=>"some-company"}

But in a controller spec I am trying to test this route and its behaviour.

get "/gb/some-company"

But I get this error

ActionController::RoutingError: No route matches {:controller=>"companies", :action=>"gb/some-comapny"}

NOTE: I have tried putting this route at the top of the routes.rb file to see if the test works but with no luck. Also all other specs are passing. Any ideas where I might be going wrong would be appreciated.

UPDATE: This is what the test looks like...

params_from(:get, "/gb/some-company").should == {:controller => "companies", :action => "show", :country_id => "gb", :id => "some-company"}

Upvotes: 3

Views: 2558

Answers (1)

Leo Correa
Leo Correa

Reputation: 19809

That's because RSpec controller tests match your request directly to the action name of the described controller.

Your test probably looks like this

describe CompaniesController do
  it "does something" do
    get "/gb/some-company"
  end
end

If you replace your get "/gb/some-company" with

get :show, {place_id: 'gb', id: 'some-company'}

I'm sure it'll work how you want it to work.

If you want to actually make the request as "/gb/some-company" through RSpec, you might want to look at request specs.

They will allow you to do something like this

describe "Company stuff" do
  it "is awesome"
    get "/gb/some-company"
  end
end

UPDATE

I'm assuming now, based on your updated question, that you're using an old version of RSpec. RSpec 2 does not currently have a params_from method

In any case, based on what you want, you want to use routing specs

The spec you want would look something like this in RSpec 2

describe "Company matching" do
  it "matches the routes" do
    expect(:get => '/gb/some-company').to route_to(:controller => "companies", :action => "show", :country_id => "gb", :id => "some-company")
  end
end

I'm not 100% sure how it would look in older versions of RSpec as I can't seem to find their docs.

Upvotes: 3

Related Questions