Module: RSpec::ComposableJSONMatchers

Defined in:
lib/rspec/composable_json_matchers.rb,
lib/rspec/composable_json_matchers/be_json.rb,
lib/rspec/composable_json_matchers/version.rb,
lib/rspec/composable_json_matchers/configuration.rb

Overview

Mix-in module for the be_json matcher.

Examples:

Make be_json matcher available everywhere

# spec/spec_helper.rb
require 'rspec/composable_json_matchers'
RSpec.configure do |config|
  include RSpec::ComposableJSONMatchers
end

Make be_json matcher available only in specific example groups

require 'rspec/composable_json_matchers'
describe 'something' do
  include RSpec::ComposableJSONMatchers
end

Defined Under Namespace

Modules: Version Classes: BeJSON, Configuration

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationRSpec::ComposableJSONMatchers::Configuration

Returns the current configuration.

Returns:

See Also:


29
30
31
# File 'lib/rspec/composable_json_matchers.rb', line 29

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... }

This method returns an undefined value.

Provides a block for configuring RSpec::ComposableJSONMatchers.

Examples:

# spec/spec_helper.rb
RSpec::ComposableJSONMatchers.configure do |config|
  config.parser_options = { symbolize_names: false }
end

Yield Parameters:

See Also:


48
49
50
# File 'lib/rspec/composable_json_matchers.rb', line 48

def configure
  yield configuration
end

.json_value?(object) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

69
70
71
72
73
74
75
76
77
78
# File 'lib/rspec/composable_json_matchers.rb', line 69

def json_value?(object)
  # value = false / null / true / object / array / number / string
  # https://www.rfc-editor.org/rfc/rfc8259.txt
  case object
  when Hash, Array, Numeric, String, TrueClass, FalseClass, NilClass
    true
  else
    false
  end
end

.matcher?(object) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

58
59
60
61
62
63
64
65
66
# File 'lib/rspec/composable_json_matchers.rb', line 58

def matcher?(object)
  begin
    return false if object.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher)
  rescue NoMethodError
    return false
  end

  object.respond_to?(:matches?)
end

.reset!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


53
54
55
# File 'lib/rspec/composable_json_matchers.rb', line 53

def reset!
  @configuration = nil
end

Instance Method Details

#be_json(matcher_or_json_value) ⇒ Object

Passes if actual string can be decoded as JSON and the decoded value passes the given matcher. When a JSON value is given, it's handled as be_json matching(value) (matching is an alias of the match matcher).

Examples:

expect('{ "foo": 1, "bar": 2 }').to be_json(foo: 1, bar: 2)
expect('{ "foo": 1, "bar": 2 }').to be_json(foo: 1, bar: a_kind_of(Integer))
expect('{ "foo": 1, "bar": 2 }').to be_json matching(foo: 1, bar: 2)
expect('{ "foo": 1, "bar": 2 }').to be_json including(foo: 1)
expect('{ "foo": 1, "bar": 2 }').to be_json a_kind_of(Hash)

Parameters:

  • matcher_or_json_value

    [#matches?, Hash, Array, Numeric, String, TrueClass, FalseClass, NilClass] a matcher object or a JSON value


99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rspec/composable_json_matchers.rb', line 99

def be_json(matcher_or_json_value)
  if ComposableJSONMatchers.matcher?(matcher_or_json_value)
    BeJSON.new(matcher_or_json_value, ComposableJSONMatchers.configuration)
  elsif ComposableJSONMatchers.json_value?(matcher_or_json_value)
    matcher = matching(matcher_or_json_value)
    BeJSON.new(matcher, ComposableJSONMatchers.configuration)
  else
    raise ArgumentError, 'You must pass a matcher or a JSON value ' \
                         '(hash, array, numeric, string, true, false, or nil) ' \
                         'to `be_json` matcher.'
  end
end