Class: GrapeEntityMatchers::RepresentMatcher::RepresentMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_entity_matchers/represent_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(representable) ⇒ RepresentMatcher

Returns a new instance of RepresentMatcher.



11
12
13
14
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 11

def initialize(representable)
  @expected_representable = representable
  RSpec::Mocks::setup
end

Instance Method Details

#as(representation) ⇒ Object



22
23
24
25
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 22

def as(representation)
  @actual_representation = representation
  self
end

#descriptionObject



74
75
76
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 74

def description
  "Ensures that #{@subject} properly obtains the value of #{@expected_representable} from a mock class."
end

#failure_messageObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 46

def failure_message
  # why did it fail???
  # 3 options, exposure, object not called, value not returned.
  # TODO: fix this
  #"Expected #{@expected_representable} to be called on mock, but it wasn't: #{@subject.exposures[@expected_representable]}"
  message = ""
  message << "#{@subject} didn't expose #{@expected_representable} as expected: #{@subject.exposures[@expected_representable]}" unless verify_exposure
  message << "#{@subject} didn't call the method #{@actual_representation || @expected_representable} to get #{@expected_representable} from the test class.\n" unless check_methods
  message << "#{@subject} didn't return the correct value for #{@expected_representable}. (#{@serialized_hash[@actual_representation || @expected_representable] } != #{@represented_attribute || :value})" unless check_value
  message
end

#failure_message_when_negatedObject



58
59
60
61
62
63
64
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 58

def failure_message_when_negated
  message = ""
  message << "Didn't expect #{@subject} to expose #{@expected_representable} correctly: #{@subject.exposures[@expected_representable]} \n" if verify_exposure
  message << "Didn't expect #{@subject} to call #{@actual_representation || @expected_representable} to get #{@expected_representable} from the test class.\n" if check_methods
  message << "Didn't expect #{@subject} to return the correct value for #{@expected_representable}. (#{@serialized_hash[@actual_representation || @expected_representable] } != #{@represented_attribute || :value})" if check_value
  message
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 16

def matches?(subject)
  @subject = subject

  check_methods && verify_exposure && check_value
end

#negative_failure_messageObject



66
67
68
69
70
71
72
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 66

def negative_failure_message
  message = ""
  message << "Didn't expect #{@subject} to expose #{@expected_representable} correctly: #{@subject.exposures[@expected_representable]} \n" if verify_exposure
  message << "Didn't expect #{@subject} to call #{@actual_representation || @expected_representable} to get #{@expected_representable} from the test class.\n" if check_methods
  message << "Didn't expect #{@subject} to return the correct value for #{@expected_representable}. (#{@serialized_hash[@actual_representation || @expected_representable] } != #{@represented_attribute || :value})" if check_value
  message
end

#using(other_entity) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 32

def using(other_entity)
  @other_entity = other_entity
  @represented_attribute  = double("RepresetedAttribute")      
  @other_entity.exposures.keys.each do |key| 
    allow(@represented_attribute ).to receive(key).and_return( @other_entity.exposures[key].nil? ? :value : nil)
  end
  self
end

#when(conditions) ⇒ Object



27
28
29
30
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 27

def when(conditions)
  @conditions = conditions
  self
end

#with_root(root) ⇒ Object



41
42
43
44
# File 'lib/grape_entity_matchers/represent_matcher.rb', line 41

def with_root(root)
  @root = root
  self
end