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) ⇒ 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



35
36
37
38
# File 'lib/json_rspec_match_maker/base.rb', line 35

def initialize(expected)
  @expected = expected
  @errors = {}
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)


17
18
19
# File 'lib/json_rspec_match_maker/base.rb', line 17

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)


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

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)


22
23
24
# File 'lib/json_rspec_match_maker/base.rb', line 22

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)


57
58
59
# File 'lib/json_rspec_match_maker/base.rb', line 57

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)


46
47
48
49
50
# File 'lib/json_rspec_match_maker/base.rb', line 46

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