我在root_path
中定义了routes.rb
,我在其他测试中调用了get root_path
,但出于某种原因,在test/controllers/application_controller_test.rb
中,我在调用{{}时遇到此错误1}}:
get root_path
这是ApplicationControllerTest#test_should_get_index_when_not_logged_in:
ActionController::UrlGenerationError: No route matches {:action=>"/", :controller=>"application"}
test/controllers/application_controller_test.rb:10:in `block in <class:ApplicationControllerTest>'
routes.rb
这是Rails.application.routes.draw do
root 'application#index'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:edit, :new, :create, :update]
resources :lessons, only: [:show, :index] do
resources :pre_lesson_surveys, shallow: true,
except: :destroy
end
end
application_controller.rb
这是class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
def index
render 'admin_home_page' if admin?
render 'user_home_page' if logged_in?
@user = User.new unless logged_in?
end
end
tests
我确定我只是在做一些愚蠢的事,但我不能把手指放在上面
答案 0 :(得分:0)
如果要确保在对根路径的请求中呈现index
模板,请求规范可能是合适的:
spec/requests/application_requests_spec.rb
:
describe "Test Root Path" do
it 'successfully renders the index template on GET /' do
get "/"
expect(response).to be_successful
expect(response).to render_template(:index)
end
end
如果您想确保index
模板在对ApplicationController
的索引操作的请求中呈现,则控制器规范可能是合适的。
spec/controllers/application_controller_spec.rb
:
describe ApplicationController do
describe "GET index" do
it "successfully renders the index template" do
expect(controller).to receive(:index)
get :index
expect(response).to be_successful
expect(response).to render_template(:index)
end
end
end