RSpec controller test fails for simple route

寵の児 提交于 2019-12-08 09:12:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!