我正在使用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"}
我会将测试#index路由添加到config / routes.rb,但它无法识别测试控制器,所以我想做类似的事情
MyApp::Application.routes.append do
controller :test do
get 'test/index' => :index
end
end
但是我不知道在哪里添加这个或者这甚至可以在rspec中使用。任何帮助都会很棒!
答案 0 :(得分:0)
如果您正在尝试测试ApplicationController,请参阅此RSpec documentation。您需要在测试中定义index
之类的方法,但效果很好。