class Gruf::Rspec::ErrorMatcher

Match errors and properly handle validations

Attributes

serialized_block[W]
serialized_block_errors[R]

Public Class Methods

new(rpc_call_proc:, expected_error_class:, serialized_block:) click to toggle source

@param [Proc] rpc_call_proc The underlying yielded controller proc to call @param [Class] expected_error_class The expected error class to occur @param [Proc|lambda|Nil] serialized_block If passed, the serialized block for inspecting errors

# 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

Public Instance Methods

error_message() click to toggle source

@return [String]

# 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?() click to toggle source

@return [Boolean]

# File lib/gruf/rspec/error_matcher.rb, line 47
def valid?
  run_rpc_call
  run_serialized_block
  error_class_matched? && !serialized_block_errors?
end

Private Instance Methods

deserialize_error() click to toggle source

Deserialize the error from the response

# File lib/gruf/rspec/error_matcher.rb, line 114
def deserialize_error
  @error_serializer&.new(@rpc_response.to_status.metadata[Gruf.error_metadata_key.to_sym])&.deserialize
end
error_class_matched?() click to toggle source

Did the error class match?

# File lib/gruf/rspec/error_matcher.rb, line 69
def error_class_matched?
  @error_class_matched
end
run_rpc_call() click to toggle source

Run the actual rpc call

# File lib/gruf/rspec/error_matcher.rb, line 92
def run_rpc_call
  @rpc_response = @rpc_call_proc.call
rescue StandardError => e
  @error_class_matched = e.is_a?(@expected_error_class)
  @rpc_response = e
end
run_serialized_block() click to toggle source

Run the serialized block, if any, and if the error class matches

# File lib/gruf/rspec/error_matcher.rb, line 102
def run_serialized_block
  return unless error_class_matched? && serialized_block?

  @serialized_block.call(deserialize_error)
  true
rescue ::RSpec::Expectations::ExpectationNotMetError, ::RSpec::Expectations::MultipleExpectationsNotMetError => e
  @serialized_block_errors << e.message
end
serialized_block?() click to toggle source

Was the method chain called with .with_serialized

@return [Boolean]

# File lib/gruf/rspec/error_matcher.rb, line 85
def serialized_block?
  @serialized_block
end
serialized_block_errors?() click to toggle source

Were there any downstream errors in the serialized block chain?

# File lib/gruf/rspec/error_matcher.rb, line 76
def serialized_block_errors?
  @serialized_block_errors.any?
end