2016-11-17 57 views
0

我遇到了一個問題,發現this answer,我相信會解決我的問題,但我無法讓它工作。這些測試爲什麼不起作用?

這裏是我的代碼:

setup do 
    # where the hell do I put this 
    def main_app 
    Rails.application.class.routes.url_helpers 
    end 
    @routes = Engine.routes 
end 

test "should get index" do 
    get :index 
    assert_response :success 
end 

[email protected]:~/sizzle-chisel$ rake test 
Started with run options --seed 60111 

ERROR["test_should_get_index", Blog::Admin::ArticlesControllerTest, 0.3109516409999742] 
test_should_get_index#Blog::Admin::ArticlesControllerTest (0.31s) 
ActionView::Template::Error:   ActionView::Template::Error: undefined method `root_path' for #<ActionDispatch::Routing:: 
RoutesProxy:0x00000006eda278> 

Blog::Engine.routes.draw do 
    root "articles#index" 

    resources :articles 

    namespace :admin do 
    resources :articles 
    root to: 'articles#index' 
    end 
end 

我不斷收到root_path是不確定的,因爲在我的佈局,有這樣的代碼:

<%= link_to "some link", main_app.root_path %> 

我的代碼庫是Rails引擎...所以當我運行rake測試時,在main_app上沒有定義root_path。

回答

0

首先,您可以刪除設置塊。

並嘗試:

test "should get index" do 
    get '/' 
    assert_response :success 
end 

root "articles#index"映射roothttp://mywebsite.com)你的網站的articles控制器上的動作index

+0

這不回答我的問題...閱讀我發佈的鏈接。我認爲你不瞭解我的問題。 –