Class: Gruf::Rspec::ErrorMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/gruf/rspec/error_matcher.rb

Overview

Match errors and properly handle validations

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rpc_call_proc:, expected_error_class:, serialized_block:) ⇒ ErrorMatcher

Returns a new instance of ErrorMatcher.

Parameters:

  • rpc_call_proc (Proc)

    The underlying yielded controller proc to call

  • expected_error_class (Class)

    The expected error class to occur

  • serialized_block (Proc|lambda|Nil)

    If passed, the serialized block for inspecting errors



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gruf/rspec/error_matcher.rb', line 32

def initialize(rpc_call_proc:,
               expected_error_class:,
               serialized_block:)
  @rpc_call_proc = rpc_call_proc
  @expected_error_class = expected_error_class
  @serialized_block = serialized_block

  @error_class_matched = false
  @error_serializer = Gruf.error_serializer
  @serialized_block_errors = []
end

Instance Attribute Details

#serialized_block=(value) ⇒ Object (writeonly)

Sets the attribute serialized_block

Parameters:

  • value

    the value to set the attribute serialized_block to.



24
25
26
# File 'lib/gruf/rspec/error_matcher.rb', line 24

def serialized_block=(value)
  @serialized_block = value
end

#serialized_block_errorsObject (readonly)

Returns the value of attribute serialized_block_errors.



25
26
27
# File 'lib/gruf/rspec/error_matcher.rb', line 25

def serialized_block_errors
  @serialized_block_errors
end

Instance Method Details

#error_messageString

Returns:

  • (String)


56
57
58
59
60
61
62
# File 'lib/gruf/rspec/error_matcher.rb', line 56

def error_message
  if serialized_block_errors?
    "Failed with serialized error validations: #{@serialized_block_errors.join("\n")}"
  else
    "Response class #{@rpc_response.class} did not match expected error class #{@expected_error_class}"
  end
end

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/gruf/rspec/error_matcher.rb', line 47

def valid?
  run_rpc_call
  run_serialized_block
  error_class_matched? && !serialized_block_errors?
end