Class: BrainzLab::Testing::Matchers::HaveBeenLoggedMatcher

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

Overview

Matcher for logs

Instance Method Summary collapse

Constructor Details

#initializeHaveBeenLoggedMatcher

Returns a new instance of HaveBeenLoggedMatcher.



191
192
193
194
# File 'lib/brainzlab/testing/matchers.rb', line 191

def initialize
  @expected_message = nil
  @expected_data = nil
end

Instance Method Details

#descriptionObject



229
230
231
232
233
234
# File 'lib/brainzlab/testing/matchers.rb', line 229

def description
  desc = "have logged at level :#{@level}"
  desc += " with message #{@expected_message.inspect}" if @expected_message
  desc += " with data #{@expected_data.inspect}" if @expected_data
  desc
end

#failure_messageObject



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/brainzlab/testing/matchers.rb', line 211

def failure_message
  actual_logs = BrainzLab::Testing.event_store.logs_at_level(@level)

  if actual_logs.empty?
    "expected a log at level :#{@level} to have been recorded, but none were found.\n" \
      "Available log levels: #{BrainzLab::Testing.event_store.logs.map { |l| l[:level] }.uniq.inspect}"
  else
    msg = "expected a log at level :#{@level}"
    msg += " with message matching #{@expected_message.inspect}" if @expected_message
    msg += " with data #{@expected_data.inspect}" if @expected_data
    msg + ", but logged messages were:\n  #{actual_logs.map { |l| { message: l[:message], data: l[:data] } }.inspect}"
  end
end

#failure_message_when_negatedObject



225
226
227
# File 'lib/brainzlab/testing/matchers.rb', line 225

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

#matches?(level) ⇒ Boolean

Returns:

  • (Boolean)


206
207
208
209
# File 'lib/brainzlab/testing/matchers.rb', line 206

def matches?(level)
  @level = level.to_sym
  BrainzLab::Testing.event_store.logged?(@level, @expected_message, @expected_data)
end

#with_data(data) ⇒ Object



201
202
203
204
# File 'lib/brainzlab/testing/matchers.rb', line 201

def with_data(data)
  @expected_data = data
  self
end

#with_message(message) ⇒ Object



196
197
198
199
# File 'lib/brainzlab/testing/matchers.rb', line 196

def with_message(message)
  @expected_message = message
  self
end