问题
This is probably a very basic error, but I am still learing. =)
My routes.rb consists only of
WebPortal::Application.routes.draw do
resources :categories
end
If I understand this correctly, this should (among others) map /categories to CategoriesController.index. This controller looks like
class CategoriesController < ApplicationController
def index
end
end
The corresponding view file exists, and rails server serves this page fine. But my RSpec test
describe CategoriesController do
describe "GET :index" do
it "should be succesful" do
get :index
response.should be_succes
end
end
end
fails with the message
Failure/Error: get :index
ActionController::RoutingError:
No route matches {:controller=>"categories"}
What am I doing wrong here?
Edit:
The command rake routes gives
rake routes
categories GET /categories(.:format) {:action=>"index", :controller=>"categories"}
POST /categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET /categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT /categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format) {:action=>"destroy", controller=>"categories"}
回答1:
I was using RSpec version 2.6.1 because I used the Gemfile from the rails tutorial at http://ruby.railstutorial.org/. Switching to version 2.7 fixed my problem.
来源:https://stackoverflow.com/questions/7859032/rspec-controller-test-fails-for-simple-route