How do you specify POST params in a Rails test?
Asked Answered
N

2

23

Working with Test::Unit and Shoulda. Trying to test Users.create. My understanding is that Rails forms send params for an object like this:

user[email]

Which turns into hash in your action, right?

params[:user][:email]

OK, so in my test I've tried...

setup { post :create, :post => { 'user[email]' => 'invalid@abc' } }

and

setup { post :create, :post => { :user => { :email => 'abc@abcd' } } }

In both cases, over in my action, params[:user] is nil.

Naidanaiditch answered 9/2, 2009 at 19:56 Comment(0)
H
46
post :create, :user => { :email => '[email protected]' }

The general form for all the test methods of get, post, put, delete are as follows:

def post(action_name, params_hash = {}, session_hash = {})

And in tests, the params hash gets directly sent into params of your controller action with no translation of any sort. Even doing integration testing you really shouldnt need to test this string to params translation as its covered very well by the rails framework tests. Plus all testing methods that need params accept a hash in this manner without complaint making things easy for you.

Handiness answered 9/2, 2009 at 19:59 Comment(1)
this also is gonna work : "post :create, { :email => '[email protected]' }" and from the controller side you will have: params[:email] gives you '[email protected]'Catheryncatheter
L
8
post :create, {:post => {}, :user => {:email => 'abc@abcd'} }

In this case params[:post] is {}, params[:user] is {:email => 'abc@abcd'}, params[:user][:email] is 'abc@abcd'.

post :create, {:post => {:user => {:email => 'abc@abcd'} } }

In this case params[:post][:user][:email] is 'abc@abcd'

Liberticide answered 25/4, 2009 at 3:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.