2015-08-17 13 views
2

我在routes.rb定義的root_path和我打電話get root_path在其他測試中,但由於某些原因,在test/controllers/application_controller_test.rb,我打電話get root_path時出現此錯誤:沒有路由錯誤抵達時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

test "should get index when not logged in" do 
    get root_path 
    assert_response :success 
    assert_not is_logged_in? 
    assert_template 'application/index' 
end 

我敢肯定,我只是在做一些愚蠢的事,但我不能把我的手指上

回答

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 
+0

我已經與測試建立主要不是使用RSpec書面工作。有沒有辦法做到這一點沒有rspec?我已經嘗試將測試更改爲「get」/「',但它不起作用,如果這有助於您嘗試 –

+1

:'get:index'? –

+0

這有效,任何想法爲什麼'得到root_path'或'get「/」'沒有工作,但這是嗎? –

相關問題