Search code examples
ruby-on-railsrubyrspecrspec-rails

RSpec - How to add default content-type?


For my project, I need to add the header HTTP_ACCEPT to all my requests.

Is there a solution to avoid this on all my controllers' specs ?

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

Solution

  • Found it !

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