jackr
jackr

Reputation: 1437

Rspec+Rails: POST URL-parameter to controller

I want my users to do

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

and arrange that the POST reaches the controller "controllername" as

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

Or, actually, I can work with any other appearance to the controller; the point is that that last word in the URL ("v1") needs to be made available to the controller for use rather similarly to p2/v2 and p3/v3.

And, I need to test this with Rpec. Specifically:

rspec 2.6.4
rails 3.0.9
ruby 1.9.2

I'm using a 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 post to it (from the controller spec). None of these work:

post :create, parameters
post :create, parameters, 'foofoo'
post :create, parameters, :p1 => 'foofoo'
post :create, :p1 => 'foofoo', parameters

Upvotes: 4

Views: 5187

Answers (1)

Crimsonknave
Crimsonknave

Reputation: 21

For your example:

Inside controllername_controller_spec.rb

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

Essentially, just put together all the parameters you would have in the path and the ones you want provided in the request body

Upvotes: 1

Related Questions