Class: JsonRspecMatchMaker::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/json_rspec_match_maker/base.rb

Overview

Base class that abstracts away all of the work of using the @match_definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expected, match_definition, prefix: '') ⇒ Base

Create a new JSON matcher

Examples:

JsonRspecMatchMaker.new(active_record_model)
JsonRspecMatchMaker.new(presenter_instance)

Parameters:

  • expected (Object)

    The object being serialized into JSON



27
28
29
30
31
32
# File 'lib/json_rspec_match_maker/base.rb', line 27

def initialize(expected, match_definition, prefix: '')
  @expected = expected
  @match_definition = expand_definition(match_definition)
  @errors = {}
  @prefix = prefix
end

Instance Attribute Details

#expectedObject (readonly)

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.

The object being expected against

Returns:

  • (Object)


9
10
11
# File 'lib/json_rspec_match_maker/base.rb', line 9

def expected
  @expected
end

#match_definitionHash (readonly)

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.

Data structure that specifies how instance values relate to JSON values

Returns:

  • (Hash)


19
20
21
# File 'lib/json_rspec_match_maker/base.rb', line 19

def match_definition
  @match_definition
end

#targetHash (readonly)

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.

The json being tested

Returns:

  • (Hash)


14
15
16
# File 'lib/json_rspec_match_maker/base.rb', line 14

def target
  @target
end

Instance Method Details

#failure_messageString

Error reporting method called by RSpec

Examples:

match_maker.failure_message #=> 'Mismatch in field name: expected (Freddy) got (Harold)'

Returns:

  • (String)


51
52
53
# File 'lib/json_rspec_match_maker/base.rb', line 51

def failure_message
  @errors.values.join('\n')
end

#matches?(target) ⇒ Bool

Match method called by RSpec

Examples:

JsonRspecMatchMaker.new(user).matches?(user.to_json) #=> true
JsonRspecMatchMaker.new(dog).matches?(cat.to_json) #=> false

Returns:

  • (Bool)


40
41
42
43
44
# File 'lib/json_rspec_match_maker/base.rb', line 40

def matches?(target)
  @target = target
  check_target_against_expected
  @errors.empty?
end