Rspec + Rails: POST URL parameter for the controller

I want my users to execute

POST /controllername/v1 { "p2":"v2", "p3":"v3" } 

and have POST reach the controller "controllername" as

  params={ :p2 => "v2", :p3 => "v3" } p1=v1 

Or, in fact, I can work with any other controller appearance; the fact is that the last word in the URL ("v1") should be available to the controller for use in approximately the same way as p2 / v2 and p3 / v3.

And I need to check this with Rpec. In particular:

 rspec 2.6.4 rails 3.0.9 ruby 1.9.2 

I am using the route

 match '/controllername/:p1' => 'controllername#create' 

And this rspec rule works:

 it 'should route to :create' do assert_routing({ :path => '/controllername/foofoo', :method => :post }, { :controller => "controllername", :action => 'create', :p1 => 'foofoo' }) end 

But I can’t figure out how to send a message (from the controller specification). None of these works:

 post :create, parameters post :create, parameters, 'foofoo' post :create, parameters, :p1 => 'foofoo' post :create, :p1 => 'foofoo', parameters 
+4
source share
1 answer

In your example:

Inside controllername_controller_spec.rb

 post :create, :p1 => "foo", :p2 => "bar", :p3 => "baz" 

Essentially, just collect all the parameters that you have in the path and those that you want to provide in the request body

+1
source

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


All Articles