Set Rspec default GET request format to JSON

前端 未结 10 1958
失恋的感觉
失恋的感觉 2020-12-02 22:31

I am doing functional tests for my controllers with Rspec. I have set my default response format in my router to JSON, so every request without a suffix will return JSON.

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 23:06

    For those folks who work with request tests the easiest way I found is to override #process method in ActionDispatch::Integration::Session and set default as parameter to :json like this:

    module DefaultAsForProcess
      def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: :json)
        super
      end
    end
    
    ActionDispatch::Integration::Session.prepend(DefaultAsForProcess)
    

提交回复
热议问题