Class: BrainzLab::Testing::LogExpectation

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

Overview

Fluent expectation builder for logs

Instance Method Summary collapse

Constructor Details

#initialize(level, store) ⇒ LogExpectation

Returns a new instance of LogExpectation.



455
456
457
458
459
460
# File 'lib/brainzlab/testing/helpers.rb', line 455

def initialize(level, store)
  @level = level.to_sym
  @store = store
  @expected_message = nil
  @expected_data = nil
end

Instance Method Details

#failure_messageObject



492
493
494
495
496
497
498
# File 'lib/brainzlab/testing/helpers.rb', line 492

def failure_message
  parts = ["expected log at level :#{@level}"]
  parts << "with message matching #{@expected_message.inspect}" if @expected_message
  parts << "with data #{@expected_data.inspect}" if @expected_data
  parts << ", but got: #{@store.logs_at_level(@level).map { |l| { message: l[:message], data: l[:data] } }.inspect}"
  parts.join
end

#failure_message_when_negatedObject



500
501
502
# File 'lib/brainzlab/testing/helpers.rb', line 500

def failure_message_when_negated
  "expected no log at level :#{@level} to be recorded, but it was"
end

#satisfied?Boolean Also known as: matches?

Check if the expectation is satisfied

Returns:

  • (Boolean)


486
487
488
# File 'lib/brainzlab/testing/helpers.rb', line 486

def satisfied?
  @store.logged?(@level, @expected_message, @expected_data)
end

#with_data(data) ⇒ self

Specify expected data

Parameters:

  • data (Hash)

    Data to match

Returns:

  • (self)


477
478
479
480
# File 'lib/brainzlab/testing/helpers.rb', line 477

def with_data(data)
  @expected_data = data
  self
end

#with_message(message) ⇒ self

Specify expected message

Parameters:

  • message (String, Regexp)

    Message to match

Returns:

  • (self)


467
468
469
470
# File 'lib/brainzlab/testing/helpers.rb', line 467

def with_message(message)
  @expected_message = message
  self
end