1

I am trying to write a failing spec for passing a invalid json when making a request to a rails api endpoint.

In a full rail api I would have used capybara and curl to make the actual request.

How do I pass a invalid json via a rspec request spec?

1

1 Answer 1

2

I ended up finding a solution to this, you need to use request specs, something along this lines:

require 'rails_helper'

RSpec.describe 'JSON formatting', :type => :request do
  context 'with bad json' do
    it 'returns 400 on bad json' do
      headers = {
          'ACCEPT' => 'application/json',
          'CONTENT_TYPE' => 'application/json'
      }
      post '/some/path', "{", headers

      expect(response.content_type).to eq("application/json")
    end
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.