3

语境

我遇到了一个非常奇怪的测试失败,我无法根据我的代码来解释。当我运行下面提供的规范测试时,它将显示以下错误:

失败:

1) GroupsController GET 'index' 返回 http 成功 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 '

测试用例

RSpec 中设置的控制器和路由的测试用例如下所示:

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

被测控制器

我已经根据测试为我的控制器编写了一个非常基本的骨架。目前它并没有做很多事情。

class GroupsController < ApplicationController
  before_filter :authenticate_user!

  def index
    @groups = current_user.groups
  end
end

配置为到达控制器的路由

routes.rb 文件如下所示:

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

  root :to => "home#welcome"
end

路线

              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

问题

我已经尝试更改路由以获取“groups”=>“groups#index”而不是资源路由并且它有效,但这不是我想要的,因为我也想将它用作 RESTful 服务。

我在这里做错了什么?

更新:添加了与组相关的路线。

4

1 回答 1

3

好吧,显然我的 Guard 和 Spork 表现得很刻薄。

一旦我重新启动 Guard/Spork,一切都按预期工作。回顾代码和配置,没有任何原因会出现问题。

因此,如果其他人遇到这种行为,请检查他们的配置和代码。重新开始!

于 2013-11-01T19:53:26.510 回答