我正在使用 rspec 为我的 rails 应用程序(用 Rails 4 编写)中的应用程序控制器编写测试,我遇到了一个问题,它无法识别我发送的 HTTP 请求的路由。我知道有一种方法可以使用 MyApp::Application.routes 来做到这一点,但我无法让它工作。
#application_controller_spec.rb
require 'spec_helper'
class TestController < ApplicationController
  def index; end
end
describe TestController do
  before(:each) do
    @first_user = FactoryGirl.create(:user) 
      # this is to ensure that all before_filters are run
    controller.stub(:first_time_user)
    controller.stub(:current_user)
end
describe 'first_time_user' do
  before(:each) do
    controller.unstub(:first_time_user)
  end
  context 'is in db' do
    before(:each) do
      @user = FactoryGirl.create(:user)
      controller.stub(:current_user).and_return(@user)
    end
    it 'should not redirect' do
      get :index
      response.should_not be_redirect
    end
  end
  context 'is not in db' do
    context 'session[:cas_user] does not exist' do
      it 'should return nil' do
        get :index
        expect(assigns(:current_user)).to eq(nil)
      end
    end
    it "should redirect_to new_user_path" do
      controller.stub(:current_user, redirect: true).and_return(nil)
      get :index
      response.should be_redirect
    end
  end
end
我现在遇到的错误是
No route matches {:action=>"index", :controller=>"test"}
我会将 test#index 路由添加到 config/routes.rb,但它无法识别测试控制器,所以我想做类似的事情
MyApp::Application.routes.append do
  controller :test do
    get 'test/index' => :index
  end
end
但我不确定在哪里添加这个,或者这是否适用于 rspec。任何帮助都会很棒!