Module: JSONAPI::Testing::TestHelper

Defined in:
lib/json_api/testing/test_helper.rb

Overview

Test helper for Rails integration/request tests that provides consistent ‘as: :jsonapi` behavior across all HTTP methods.

Usage in RSpec:

RSpec.configure do |config|
  config.include JSONAPI::Testing::TestHelper, type: :request
end

Then use ‘as: :jsonapi` in your tests:

get users_path, params: { filter: { active: true } }, headers:, as: :jsonapi
post users_path, params: payload, headers:, as: :jsonapi

Behavior:

  • GET: Sets Accept header, params go to query string (no body encoding)

  • POST/PATCH/PUT/DELETE: Sets Content-Type and Accept headers, JSON-encodes body

Constant Summary collapse

JSONAPI_MIME =
"application/vnd.api+json"

Instance Method Summary collapse

Instance Method Details

#delete(path, **options) ⇒ Object



47
48
49
50
# File 'lib/json_api/testing/test_helper.rb', line 47

def delete(path, **options)
  options = apply_jsonapi_options_for_body(options) if options[:as] == :jsonapi
  super
end

#get(path, **options) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/json_api/testing/test_helper.rb', line 24

def get(path, **options)
  if options[:as] == :jsonapi
    options = apply_jsonapi_headers(options, include_content_type: false)
    options.delete(:as)
  end
  super
end

#patch(path, **options) ⇒ Object



37
38
39
40
# File 'lib/json_api/testing/test_helper.rb', line 37

def patch(path, **options)
  options = apply_jsonapi_options_for_body(options) if options[:as] == :jsonapi
  super
end

#post(path, **options) ⇒ Object



32
33
34
35
# File 'lib/json_api/testing/test_helper.rb', line 32

def post(path, **options)
  options = apply_jsonapi_options_for_body(options) if options[:as] == :jsonapi
  super
end

#put(path, **options) ⇒ Object



42
43
44
45
# File 'lib/json_api/testing/test_helper.rb', line 42

def put(path, **options)
  options = apply_jsonapi_options_for_body(options) if options[:as] == :jsonapi
  super
end