Module: JSONSpectacular::RSpec

Defined in:
lib/json_spectacular/rspec.rb

Overview

Module containing JSON helper methods that can be mixed into RSpec test scope

Instance Method Summary collapse

Instance Method Details

#eql_json(expected) ⇒ JSONSpectacular::Matcher

Creates a new JSONSpectacular::Matcher instance so it can be passed to RSpec to match expected against an actual value.

Examples:

Use the eql_json expectation

expect(actual).to eql_json(expected)

Use the eql_json expectation with json_response

expect(json_response).to eql_json(expected)

Parameters:

  • expected (Boolean, Hash, String, Number, Array)

    The expected value the RSpec matcher should match against.

Returns:

See Also:



34
35
36
# File 'lib/json_spectacular/rspec.rb', line 34

def eql_json(expected)
  JSONSpectacular::Matcher.new(expected)
end

#json_responseHash{String => Boolean, String, Number, Hash, Array}

Parses the last response body in a Rails RSpec controller or request test as JSON

Returns:

  • (Hash{String => Boolean, String, Number, Hash, Array})

    Ruby representation of the JSON response body.



14
15
16
17
18
# File 'lib/json_spectacular/rspec.rb', line 14

def json_response
  JSON.parse(response.body)
rescue JSON::ParserError
  '< INVALID JSON RESPONSE >'
end