No query parameters in the restriction when testing the route

When I run this in practice, it works, but I cannot write a working test to limit the route using rspec.

When the test starts, the restriction starts, but the request parameters are empty, so it does not check and the test fails.

I am running Rails 3.0.9, rspec-rails 2.6.1 and rspec 2.6.0.

configurations /routes.rb

match ":param1-unique-:param2" => "controller#index", :constraints => ParamConstraint.new 

Library / param _constraint.rb

 class ParamConstraint def matches?(request) @request ||= request valid_param1? && valid_param2? end def valid_param1? @request.params[:param1] == "lorem" end def valid_param2? @request.params[:param2] == "ipsum" end end 

specifications / routing / param_constraint_spec.rb

 require 'spec_helper' describe "param constraint routing" do it "recognizes route for param1 and param2" do { :get => "/lorem-unique-ipsum" }. should route_to( :controller => "controller", :action => "index", :param1 => "lorem", :param2 => "ipsum" ) end end 

Update

If I check the request in the constraint, I get the following output:

 #<ActionDispatch::Request:0x007fee140ff910 @env={ "rack.version"=>[1, 1], "rack.input"=>#<StringIO:0x007fee1446da48>, "rack.errors"=>#<StringIO:0x007fee1446e768>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "PATH_INFO"=>"/lorem-unique-ipsum", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0" }> 
+4
source share
2 answers

Today I ran into this problem, and finding an answer led me to this question. For what it was worth, I had to resort to writing the specification of the request.

 context "passing params that satisfy ParamConstraint" do before do visit "/lorem-unique-ipsum" end it "should serve up a page with content" do # replace this with some assertion that gets satisfied by # pages served up when ParamConstraint.new.matches? returns true page.should have_selector("html body div#foo") page.should_not have_selector("html body div#bar") end end context "passing params that DO NOT satisfy ParamConstraint" do before do visit "/other-unique-other" end it "should serve up a page with different content" do # replace this with some assertion that gets satisfied by # pages served up when ParamConstraint.new.matches? returns false page.should_not have_selector("html body div#foo") page.should have_selector("html body div#bar") end end 

This does not answer your question, which I perceive as "how to check the routing restriction", since the correct way would be to use the routing specification. But given this gap in how request.params works when you use "should route_to", this is a workaround. The request specification, unlike the routing specification, will populate request.params correctly.

+2
source

The same problem exists a few years later, with rspec-core 3.4.4, rspec-rails 3.4.2, rails 4.2.6. Do not have time to understand why ...

You can use the request specification as suggested above, but do not use it to check the contents of the page. Instead, repeat the routing test ( route_to ), checking the translation of the URL paths to request the parameters:

 RSpec.describe 'routes', type: :request do describe '/:slug' do it 'routes correctly' do get '/test-product-slug' expect(request.params).to eq( 'controller' => 'product', 'action' => :index, 'slug' => 'test-product-slug' ) end end end 
0
source

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


All Articles