【问题标题】:RSpec controller test fails for simple route简单路由的 RSpec 控制器测试失败
【发布时间】:2011-10-22 11:04:23
【问题描述】:

这可能是一个非常基本的错误,但我仍在学习。 =)

我的 routes.rb 只包含

WebPortal::Application.routes.draw do
  resources :categories
end

如果我理解正确,这应该(除其他外)将/categories 映射到CategoriesController.index。这个控制器看起来像

class CategoriesController < ApplicationController
  def index
  end
end

对应的视图文件存在,rails server 可以正常服务这个页面。但是我的 RSpec 测试

describe CategoriesController do
  describe "GET :index" do
    it "should be succesful" do
      get :index
      response.should be_succes
    end
  end
end

消息失败

Failure/Error: get :index
    ActionController::RoutingError:
    No route matches {:controller=>"categories"}

我在这里做错了什么?

编辑:

rake routes 给出的命令

 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"}

【问题讨论】:

  • 在项目文件夹中运行“rake routes”有什么好处?
  • @Rasmus:我用输出编辑了我的问题。
  • 我自己还是新手,我正在研究一个简单的项目。我的代码和你的代码之间的唯一区别是我在规范中将索引设置为“索引”而不是 :index。但我认为不会这样做

标签: ruby-on-rails unit-testing rspec controller


【解决方案1】:

我使用的是 RSpec 版本 2.6.1,因为我使用了 http://ruby.railstutorial.org/ 的 rails 教程中的 Gemfile。切换到 2.7 版解决了我的问题。

【讨论】:

  • 版本 2.6.1 什么?到 2.7 版什么的?
  • 是的,这有点缺失。 =) RSpec.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多