Class: RSpec::Kwalify::HaveError

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec-kwalify.rb

Overview

Author:

  • Martin Englund

Instance Method Summary collapse

Constructor Details

#initialize(error, base_error = ::Kwalify::BaseError) ⇒ HaveError

Returns a new instance of HaveError.



9
10
11
12
# File 'lib/rspec-kwalify.rb', line 9

def initialize(error, base_error=::Kwalify::BaseError)
  @error = error
  @base_error = base_error
end

Instance Method Details

#failure_message_for_shouldObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec-kwalify.rb', line 30

def failure_message_for_should
  msg = "expected errors to contain '%s' but was '%s'"
  if @error.nil?
    errors = @errors.map { |e| e.class.name }.uniq
    msg % [@base_error, errors.join(", ")]
  elsif @error.is_a?(Regexp)
    errors = @errors.map { |e| e.message }.uniq
    msg % [@error.source, errors.join(", ")]
  elsif @error.is_a?(Fixnum)
    "expected #{@error} errors, found #{@errors.size}"
  end
end

#matches?(errors) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rspec-kwalify.rb', line 14

def matches?(errors)
  @errors = errors

  if @error.nil?
    @errors.any? { |error| error.is_a?(@base_error) }
  elsif @error.is_a?(Regexp)
    @errors.any? do |error|
      error.is_a?(@base_error) && error.message.match(@error)
    end
  elsif @error.is_a?(Fixnum)
    @errors.size == @error
  else
    raise ArgumentError, "don't know how to handle a #{@error.class}"
  end
end