How can I check rspec controller with routing

To get a list of questionnaires, I use

GET "/questionnaires/user/1/public/true/mine/true/shared/true" 

in routes.rb I have

 /questionnaires/*myparams(.:format) {:controller=>"questionnaires", :action=>"list"} 

The controller uses routing to create a request in the list method.

 class QuestionnairesController < ApplicationController before_filter :authenticate def list myparams = params[:myparams].split("/").to_h end # ... end 

I am trying to create test cases for all parameters in a specification file

 describe "GET list" do it "returns the list of questionnaires for the user" do get :list # ... end end 

what i get when i run rspec

 Failures: 1) QuestionnairesController List GET list returns the list of questionnaires for the user Failure/Error: get :list No route matches {:controller=>"questionnaires", :action=>"list"} # ./spec/controllers/questionnaires_controller_spec.rb:20 

The question is how do you write the spec file to pass the globbed parameters to rspec. I like to do something like this:

 describe "GET list" do it "returns the list of questionnaires for the user" do get :list, "/user/1/public/true/mine/true/shared/true" end end 

and change the settings to test various cases

+4
source share
1 answer

The globe occurs in the dispatcher, so the parameters are already assigned when the controller is called. When the controller action is reached, the globbed parameters should already be split into an array in params[:myparams] .

If you want to test this in the controller, just configure the hash parameter like the dispatcher:

 describe "GET 'list'" do it "should be successful" do get :list, :myparams => "user/1/public/true/mine/true/shared/true".split("/") response.should be_success end end 
+1
source

Source: https://habr.com/ru/post/1340106/


All Articles