Class: BrainzLab::Testing::ErrorExpectation

Inherits:
Object
  • Object
show all
Defined in:
lib/brainzlab/testing/helpers.rb

Overview

Fluent expectation builder for errors

Instance Method Summary collapse

Constructor Details

#initialize(error_class, store) ⇒ ErrorExpectation

Returns a new instance of ErrorExpectation.



403
404
405
406
407
408
# File 'lib/brainzlab/testing/helpers.rb', line 403

def initialize(error_class, store)
  @error_class = error_class
  @store = store
  @expected_message = nil
  @expected_context = nil
end

Instance Method Details

#failure_messageObject



440
441
442
443
444
445
446
# File 'lib/brainzlab/testing/helpers.rb', line 440

def failure_message
  parts = ["expected error #{@error_class} to be captured"]
  parts << "with message matching #{@expected_message.inspect}" if @expected_message
  parts << "with context #{@expected_context.inspect}" if @expected_context
  parts << ", but got: #{@store.errors.map { |e| { class: e[:error_class], message: e[:message] } }.inspect}"
  parts.join
end

#failure_message_when_negatedObject



448
449
450
# File 'lib/brainzlab/testing/helpers.rb', line 448

def failure_message_when_negated
  "expected error #{@error_class} not to be captured, but it was"
end

#satisfied?Boolean Also known as: matches?

Check if the expectation is satisfied

Returns:

  • (Boolean)


434
435
436
# File 'lib/brainzlab/testing/helpers.rb', line 434

def satisfied?
  @store.error_captured?(@error_class, message: @expected_message, context: @expected_context)
end

#with_context(context) ⇒ self

Specify expected context

Parameters:

  • context (Hash)

    Context to match

Returns:

  • (self)


425
426
427
428
# File 'lib/brainzlab/testing/helpers.rb', line 425

def with_context(context)
  @expected_context = context
  self
end

#with_message(message) ⇒ self

Specify expected message

Parameters:

  • message (String, Regexp)

    Message to match

Returns:

  • (self)


415
416
417
418
# File 'lib/brainzlab/testing/helpers.rb', line 415

def with_message(message)
  @expected_message = message
  self
end