我遇到了一个问题并找到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
blaine@vm:~/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 :(得分:0)
首先,您可以删除设置块。
尝试:
test "should get index" do
get '/'
assert_response :success
end
root "articles#index"
将您网站的root
(http://mywebsite.com)映射到index
控制器上的操作articles
。