Set Rspec default GET request format to JSON

前端 未结 10 1911
失恋的感觉
失恋的感觉 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 22:52

    Perhaps you could add the first answer into spec/spec_helper or spec/rails_helper with this:

    config.before(:each) do
      request.env["HTTP_ACCEPT"] = 'application/json' if defined? request
    end
    

    if in model test (or any not exist request methods context), this code just ignore. it worked with rspec 3.1.7 and rails 4.1.0 it should be worked with all rails 4 version generally speaking.

提交回复
热议问题