Class: BrainzLab::Testing::Matchers::HaveBeenCapturedMatcher

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

Overview

Matcher for errors

Instance Method Summary collapse

Constructor Details

#initializeHaveBeenCapturedMatcher

Returns a new instance of HaveBeenCapturedMatcher.



140
141
142
143
# File 'lib/brainzlab/testing/matchers.rb', line 140

def initialize
  @expected_message = nil
  @expected_context = nil
end

Instance Method Details

#descriptionObject



181
182
183
184
185
186
# File 'lib/brainzlab/testing/matchers.rb', line 181

def description
  desc = "have captured error #{@error_class}"
  desc += " with message #{@expected_message.inspect}" if @expected_message
  desc += " with context #{@expected_context.inspect}" if @expected_context
  desc
end

#failure_messageObject



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/brainzlab/testing/matchers.rb', line 164

def failure_message
  actual_errors = BrainzLab::Testing.event_store.errors

  if actual_errors.empty?
    "expected error #{@error_class} to have been captured, but no errors were captured"
  else
    msg = "expected error #{@error_class} to have been captured"
    msg += " with message matching #{@expected_message.inspect}" if @expected_message
    msg += " with context #{@expected_context.inspect}" if @expected_context
    msg + ", but captured errors were:\n  #{actual_errors.map { |e| { class: e[:error_class], message: e[:message] } }.inspect}"
  end
end

#failure_message_when_negatedObject



177
178
179
# File 'lib/brainzlab/testing/matchers.rb', line 177

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

#matches?(error_class) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
162
# File 'lib/brainzlab/testing/matchers.rb', line 155

def matches?(error_class)
  @error_class = error_class
  BrainzLab::Testing.event_store.error_captured?(
    @error_class,
    message: @expected_message,
    context: @expected_context
  )
end

#with_context(context) ⇒ Object



150
151
152
153
# File 'lib/brainzlab/testing/matchers.rb', line 150

def with_context(context)
  @expected_context = context
  self
end

#with_message(message) ⇒ Object



145
146
147
148
# File 'lib/brainzlab/testing/matchers.rb', line 145

def with_message(message)
  @expected_message = message
  self
end