尝试测试命名空间控制器时的ActionController :: UrlGenerationError

时间:2016-05-20 14:47:34

标签: ruby-on-rails ruby rspec

我试图为我在Front命名空间下定义的控制器操作之一编写一个控制器规范(并且生活在root rails app中的Front引擎中)。我一直在关注这件事:https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs/engine-routes-for-controllers

这是我的代码:

# config/routes/rb
Rails.application.routes.draw do
  mount Front::Engine => '/', as: 'front'
end
# modules/front/config/routes.rb
Front::Engine.routes.draw do
  resources :items
end
# modules/front/app/controllers/front/items_controller.rb
class Front::ItemsController < Front::ApplicationController
  def index
    # ...
  end
end
# spec/controllers/front/items_controller_spec.rb
require 'rails_helper'

RSpec.describe Front::ItemsController, type: :controller do
  routes { Front::Engine.routes }

  describe "GET index" do
    it "assings @items" do
      item = FactoryGirl.create(:item)
      get :index
      expect(assigns(:items)).to eq([item])
    end
  end
end

运行以上规格给我:

Front::ItemsController
  GET index
    assings @items (FAILED - 1)

Failures:

  1) Front::ItemsController GET index assings @items
     Failure/Error: get :index

     ActionController::UrlGenerationError:
       No route matches {:action=>"index", :controller=>"front/items"}

一切都在规格之外工作正常,我可以完全访问该端点。任何想法在这里有什么不对?提前谢谢。

  • ruby​​ 2.3.1
  • rspec 3.4.4
  • rails 4.2.1

0 个答案:

没有答案