ruby on rails - RSpec "No route matches" error but they match -
i have tests those:
rspec.describe restaurscontroller, :type => :controller context "get /restaurs/:id" before @rest = factorygirl.create :restaur end context "resource exists" subject { "restaurs/#{@rest.id}"} { expect(subject).to render_template(:show) } end context "resource doesn't exists" subject { "restaurs/#{@rest.id + 1}" } { expect(subject).to redirect_to(:root) } end end end when run tests rspec says:
failure/error: subject { "restaurs/#{@rest.id}"} actioncontroller::urlgenerationerror: no route matches {:action=>"restaurs/7", :controller=>"restaurs"} but think routes ok. on rake routes log
prefix verb uri pattern controller#action restaurs /restaurs(.:format) restaurs#index post /restaurs(.:format) restaurs#create new_restaur /restaurs/new(.:format) restaurs#new edit_restaur /restaurs/:id/edit(.:format) restaurs#edit restaur /restaurs/:id(.:format) restaurs#show patch /restaurs/:id(.:format) restaurs#update set /restaurs/:id(.:format) restaurs#update delete /restaurs/:id(.:format) restaurs#destroy root / restaurs#index have got ideas? problem? maybe rspec syntax or that?
that isn't how get actions in controller test. argument get name of action, not routable path. routing isn't involved in testing controllers, , neither rake routes.
you're giving action name "restaurs/7", isn't real name of method on controller. instead, should using get :show invoke actual method "show" on controller, , passing hash of parameters:
get :show, id: @rest.id + 1 similarly, utilize get :index, not get "/", , utilize get :edit, id: ..., not get "/restaurs/#{...}/edit". it's actual method meant test on controller, not whether route leads right method.
as related aside, opening context out of place. shouldn't using get /restaurs:/:id context. routing isn't involved here. test method names.
ruby-on-rails rspec
No comments:
Post a Comment