Willem Meints
Willem Meints

Reputation: 1182

RSpec test fails with "No route matches" for resources route

Context

I'm running into a very weird test failure that I can't explain based on my code. When I run the spec test provided below, it will display the following error:

Failures:

1) GroupsController GET 'index' returns http success Failure/Error: get 'index' ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"groups"} # ./spec/controllers/groups_controller_spec.rb:14:in `block (3 levels) in '

Test case

The test case for the controller and routes set in RSpec looks like this:

describe GroupsController do

  before :each do
    @group = FactoryGirl.create(:group)
    @user = FactoryGirl.create(:user)

    sign_in @user
  end

  describe "GET 'index'" do
    it "returns http success" do
      get 'index'
      response.should be_success
    end
  end
end

Controller under test

I've written a very basic skeleton for my controller based on the test. Currently it doesn't do a whole lot of stuff.

class GroupsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @groups = current_user.groups
  end
end

Routes configured to reach the controller

The routes.rb file looks like this:

NerdCooking::Application.routes.draw do
  resources :groups
  devise_for :users

  root :to => "home#welcome"
end

Routes

              groups GET    /groups(.:format)              groups#index
                     POST   /groups(.:format)              groups#create
           new_group GET    /groups/new(.:format)          groups#new
          edit_group GET    /groups/:id/edit(.:format)     groups#edit
               group GET    /groups/:id(.:format)          groups#show
                     PATCH  /groups/:id(.:format)          groups#update
                     PUT    /groups/:id(.:format)          groups#update
                     DELETE /groups/:id(.:format)          groups#destroy

Question

I have tried changing the route to get "groups" => "groups#index" instead of the resources route and that works, but it's not something I want since I want to use this as a RESTful service as well.

What am I doing wrong here?

Update: Added the routes related to groups.

Upvotes: 3

Views: 2652

Answers (1)

Willem Meints
Willem Meints

Reputation: 1182

Okay, apparently my Guard and Spork were acting mean.

Once I restarted Guard/Spork and it all worked as expected. Looking back at the code and configuration there was no reason why stuff was going wrong.

So if anyone else is experiencing this behavior and their config and code check out. Restart!

Upvotes: 3

Related Questions