How to send raw post data in a Rails functional test?

后端 未结 12 1606
感情败类
感情败类 2020-12-13 12:07

I\'m looking to send raw post data (e.g. unparamaterized JSON) to one of my controllers for testing:

class LegacyOrderUpdateControllerTest < ActionControl         


        
12条回答
  •  一整个雨季
    2020-12-13 12:37

    I actually solved the same issues just adding one line before simulating the rspec post request. What you do is to populate the "RAW_POST_DATA". I tried to remove the attributes var on the post :create, but if I do so, it do not find the action.

    Here my solution.

    def do_create(attributes)
      request.env['RAW_POST_DATA'] = attributes.to_json
      post :create, attributes
    end 
    

    In the controller the code you need to read the JSON is something similar to this

      @property = Property.new(JSON.parse(request.body.read))
    

提交回复
热议问题