How do you specify POST params in a Rails test?

后端 未结 2 776
灰色年华
灰色年华 2021-01-31 18:27

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[ema         


        
相关标签:
2条回答
  • 2021-01-31 19:14
    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'

    0 讨论(0)
  • 2021-01-31 19:24
    post :create, :user => { :email => 'foo@bar.com' }
    

    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.

    0 讨论(0)
提交回复
热议问题