Class: PencilPusher::HaveErrors

Inherits:
Object
  • Object
show all
Defined in:
lib/pencil_pusher/have_errors.rb

Instance Method Summary collapse

Constructor Details

#initialize(field_name, expected_errors = []) ⇒ HaveErrors

Returns a new instance of HaveErrors.



3
4
5
6
# File 'lib/pencil_pusher/have_errors.rb', line 3

def initialize(field_name, expected_errors=[])
  @field_name = field_name
  @expected_errors = expected_errors
end

Instance Method Details

#descriptionObject



33
34
35
# File 'lib/pencil_pusher/have_errors.rb', line 33

def description
  "have errors for #{field_name}"
end

#failure_message_for_shouldObject Also known as: failure_message



18
19
20
21
22
23
24
# File 'lib/pencil_pusher/have_errors.rb', line 18

def failure_message_for_should
  if errors = actual.error_messages[field_name].presence
    "expected field #{field_name} to have errors #{expected_errors} but had errors #{errors}"
  else
    "expected field #{field_name} to have errors #{expected_errors} but had no errors"
  end
end

#failure_message_for_should_notObject Also known as: failure_message_when_negated



26
27
28
# File 'lib/pencil_pusher/have_errors.rb', line 26

def failure_message_for_should_not
  "expected no errors for field #{field_name} but found #{actual.error_messages[field_name]}"
end

#matches?(form) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
# File 'lib/pencil_pusher/have_errors.rb', line 8

def matches?(form)
  @actual = form
  valid = form.valid?
  if expected_errors.present?
    form.error_messages[field_name] == expected_errors
  else
    form.error_messages[field_name].present?
  end
end