Rspec在控制器规范中没有路由匹配

时间:2017-06-20 09:20:34

标签: 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(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'

1 个答案:

答案 0 :(得分:0)

您可以通过将路线更改为:

来使其发挥作用
 scope '/accounts' do
   get '/' => 'accounts#index', as: :account
 end 

我很好奇,如果Account是资源,为什么你不会将帐户声明为资源:

 resource :accounts, :only => [:show, :index]