Ruby on rails Rspec控制器规范中没有路由匹配
我有一条路线:Ruby on rails Rspec控制器规范中没有路由匹配,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我有一条路线: scope '/account' do get '/' => 'accounts#index', as: :account end 我想用RSpec测试它: describe AccountsController, type: :controller do it 'renders the index template' do get :index expect(response.status).to eq(200) expect(resp
scope '/account' do
get '/' => 'accounts#index', as: :account
end
我想用RSpec测试它:
describe AccountsController, type: :controller do
it 'renders the index template' do
get :index
expect(response.status).to eq(200)
expect(response).to render_template :index
end
end
但我有:
ActionController::UrlGenerationError:没有路由匹配
如何修复它
完整跟踪:
[11] pry(#<RSpec::ExampleGroups::AccountsController>)> get :index
ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"accounts"}
from /home/rubydev/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.5/lib/action_dispatch/journey/formatter.rb:46:in `generate'
[11]pry(#)>get:index
ActionController::UrlGenerationError:没有路由匹配{:操作=>“索引”,:控制器=>“帐户”}
from/home/rubydev/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.5/lib/action\u dispatch/journey/formatter.rb:46:in“generate”
您可以将路由更改为:
scope '/accounts' do
get '/' => 'accounts#index', as: :account
end
但是我很好奇,如果帐户
是资源,为什么不将帐户声明为资源:
resource :accounts, :only => [:show, :index]
请共享错误跟踪?@NarasimhaReddy updated您可以提供整个测试文件吗,我已经测试了您的代码,它可以工作,哪个Rails、Ruby和RSpec版本?您是否尝试在范围中使用
'/accounts'
?为什么我应该尝试?我想您需要获取:index,scope:'/accounts'。但是就像@moyinho20建议你应该使用资源。而不是问“我为什么要尝试它?”。。。相反,你应该考虑每个建议的利弊。而不是说“我只是支持它”…也许你应该尝试改进代码。如果您的团队中有人编写的代码很难维护,而不是像这样保留,您应该始终尝试改进代码,因为即使您现在忽略它,将来您也可能不得不再次回到同一行。@Sts,因为它可能会工作吗?否则我不知道你到底想修什么。我想不出我的答案有什么改进。谢谢