How to post JSON data in rails 3 functional test

前端 未结 7 1823
礼貌的吻别
礼貌的吻别 2021-02-04 06:55

I plan to use JSON data in both request and response in my project and having some problems in testing.

After searching for a while, I find the following code which uses

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-04 07:45

    As @taro suggests in a comment above, the syntax that works for me in functional and integration tests is:

    post :create, {param1: 'value1', param2: 'value2', format: 'json'}
    

    (The curly braces aren't always necessary, but sometimes it doesn't work if they're missing, so I always add them.)

    Here's what params and request.format look like for a post of that sort:

    params: {"param1"=>"value1", "param2"=>"value2", "format"=>"json", "controller"=>"things", "action"=>"create"}

    request.format: application/json

提交回复
热议问题