Class: RspecModelValidations::Matchers::Invalidate

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/rspec_model_validations/matchers/invalidate.rb

Overview

Test if model validation lead to errors on the given attribute.

Option:

* `with` test that the attribute have specific error rather than any

Instance Method Summary collapse

Methods included from Base

#on

Constructor Details

#initialize(attribute) ⇒ Invalidate



10
11
12
13
14
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 10

def initialize attribute
  super

  @with = nil
end

Instance Method Details

#descriptionString

One line syntax description



64
65
66
67
68
69
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 64

def description
  result = "invalidate #{@attribute.inspect} attribute"
  result = "#{result} with #{@with.map(&:inspect).join ', '}" unless @with.nil?

  result
end

#failure_messageString

Explain why matches? fail



40
41
42
43
44
45
46
47
48
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 40

def failure_message
  attribute = "#{@model.class}##{@attribute}"
  value = attribute_value(@model).inspect

  expected = "#{value} on #{attribute} to be invalidated"
  expected = "#{expected} with #{@with.map(&:inspect).join ', '}" unless @with.nil?

  message expected, @errors
end

#failure_message_when_negatedString

Explain why matches? fail when negated



52
53
54
55
56
57
58
59
60
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 52

def failure_message_when_negated
  attribute = "#{@model.class}##{@attribute}"
  value = attribute_value(@model).inspect

  expected = "#{value} on #{attribute} to be validated"
  expected = "#{value} on #{attribute} not to be invalidated with #{@with.map(&:inspect).join ', '}" unless @with.nil?

  message expected, @errors
end

#matches?(model) ⇒ Boolean

Run the test

Raises:



29
30
31
32
33
34
35
36
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 29

def matches? model
  attribute_belongs_to! model

  @model = model
  @errors = attribute_errors model

  @errors.count != 0 && (@with.nil? || (@with & @errors) == @with)
end

#with(*errors) ⇒ self

Set the with option



19
20
21
22
23
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 19

def with *errors
  @with = errors

  self
end