2

When building my app, I generated scaffolds, which created standard Rspec tests. I'd like to use these tests for coverage, but they seem to be failing due to nested routes:

When I run the test, this is its feedback:

Failures:

  1) ListItemsController routing routes to #index
     Failure/Error: get("/list_items").should route_to("list_items#index")
       No route matches "/list_items"
     # ./spec/routing/list_items_routing_spec.rb:7:in `block (3 levels) in <top (required)>'

Finished in 0.25616 seconds
1 example, 1 failure

How do I tell Rspec that there are nested routes?

Here are the abridged files:

list_items_routing_spec.rb:

require "spec_helper"

describe ListItemsController do
  describe "routing" do

    it "routes to #index" do
      get("/list_items").should route_to("list_items#index")
    end

end

list_items_controller_spec.rb:

describe ListItemsController do
  # This should return the minimal set of attributes required to create a valid
  # ListItem. As you add validations to ListItem, be sure to
  # adjust the attributes here as well.
  let(:valid_attributes) { { "list_id" => "1", "project_id" => "1"  } }

  # This should return the minimal set of values that should be in the session
  # in order to pass any filters (e.g. authentication) defined in
  # ListItemsController. Be sure to keep this updated too.
  let(:valid_session) { {} }

  describe "GET index" do
    it "assigns all list_items as @list_items" do
      list_item = ListItem.create! valid_attributes
      get :index, project_id: 2, {}, valid_session
      assigns(:list_items).should eq([list_item])
    end
  end

routes.rb:

  resources :projects do
    member do
      match "list_items"
    end
  end

Notes: - I've tried changing the rpec tests themselves to include a project_id, and that didn't help. - I'm using Factory Girl for fixture generation (not sure if this is relevant)

Thanks for your help!

1 Answer 1

4

First of all, run rake routes to see what routes exist.

According to what you have in your routes I would expect you have a ProjectsController that has an action list_items. This action would be available under /projects/:id/list_items.

Now I can only theorize about what really you want, but I will guess.

If you want /projects/:project_id/list_items to route to list_items#index you have to change your routes to:

resources :projects do
    resources :list_items
end

You can confirm that by running rake routes.

Then fix the assertion in your routing spec:

get("/projects/23/list_items").should route_to("list_items#index", :project_id => "23")

Update for RSpec v2.14+ Expectations

expect(:get => "/projects/23/list_items").to route_to("list_items#index", :project_id => "23")
3
  • Hi Dave, I think I necessarily have to have "member do" in my routes file to properly nest and access these files (otherwise I get routing failures). Is there a way to relay this Member relationship to Rspec? Currently when I run the assertion you gave me, it fails with: Failure/Error: get("/projects/23/list_items").should route_to("list_items#index", :project_id => "23") The recognized options <{"action"=>"index", "controller"=>"list_items", "id"=>"23"}> did not match <{"project_id"=>"23", "controller"=>"list_items", "action"=>"index"}>, difference: <{"project_id"=>"23", "id"=>"23"}>. Jul 15, 2013 at 22:37
  • Look. It's your code. Do necessarily what you want. With the member route you the router will route the request to projects#list_items which is not what you want I suppose. See Rails Guides on how routing works and how to nest resources: guides.rubyonrails.org/routing.html#nested-resources
    – DavidRH
    Jul 16, 2013 at 10:36
  • 1
    I've just noticed that the number representing the id must be in string format. In my case, @director.id.to_s Feb 19, 2016 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.