RSpec - 如何添加默认 content-type?

RSpec - How to add default content-type?

对于我的项目,我需要将 header HTTP_ACCEPT 添加到我的所有请求中。

是否有解决方案可以避免我所有控制器的规格出现这种情况?

before do
    @request.env["HTTP_ACCEPT"] = 'application/json'
end

找到了!

Rspec.configure do |config|
  config.before(:each) do |example|
    if example.metadata[:type] == :controller
      controller.request.env["HTTP_ACCEPT"] = 'application/json'
    end
  end
end