Class: Brut::SpecSupport::Matchers::HaveConstraintViolation

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/spec_support/matchers/have_constraint_violation.rb

Overview

Matcher to check that a from has a specific constraint violation.

Examples:

expect(form).to have_constraint_violation(:email, key: :required)

Index fields (requires that the third email field have a constraint violation)

expect(form).to have_constraint_violation(:email, key: :required, index: 2)

Negated

expect(form).not_to have_constraint_violation(:email, key: :required)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, field, key, index) ⇒ HaveConstraintViolation

Returns a new instance of HaveConstraintViolation.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brut/spec_support/matchers/have_constraint_violation.rb', line 46

def initialize(form, field, key, index)
  if !form.kind_of?(Brut::FrontEnd::Form)
    raise "#{self.class} only works with forms, not #{form.class}"
  end
  @form  = form
  @field = field.to_s
  @key   = key.to_s
  @index = index || 0

  @matches             = false
  @found_field         = false
  @fields_found        = Set.new
  @keys_on_field_found = Set.new

  @form.constraint_violations.each do |input_name, (constraint_violations, index)|
    if input_name.to_s == @field && index == @index
      @found_field = true
      constraint_violations.each do |constraint_violation|
        if constraint_violation.key.to_s == @key
          @matches = true
        else
          @keys_on_field_found << constraint_violation.key.to_s
        end
      end
    else
      @fields_found << [ input_name.to_s, index ]
    end
  end
end

Instance Attribute Details

#fields_foundObject (readonly)

Returns the value of attribute fields_found.



43
44
45
# File 'lib/brut/spec_support/matchers/have_constraint_violation.rb', line 43

def fields_found
  @fields_found
end

#keys_on_field_foundObject (readonly)

Returns the value of attribute keys_on_field_found.



44
45
46
# File 'lib/brut/spec_support/matchers/have_constraint_violation.rb', line 44

def keys_on_field_found
  @keys_on_field_found
end

Instance Method Details

#found_field?Boolean

Returns:

  • (Boolean)


77
# File 'lib/brut/spec_support/matchers/have_constraint_violation.rb', line 77

def found_field?  = @found_field

#matches?Boolean

Returns:

  • (Boolean)


76
# File 'lib/brut/spec_support/matchers/have_constraint_violation.rb', line 76

def matches?      = @matches