Class: RspecModelValidations::Matchers::Invalidate
- Inherits:
-
Object
- Object
- RspecModelValidations::Matchers::Invalidate
- 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
-
#description ⇒ String
One line syntax description.
-
#failure_message ⇒ String
Explain why matches? fail.
-
#failure_message_when_negated ⇒ String
Explain why matches? fail when negated.
-
#initialize(attribute) ⇒ Invalidate
constructor
A new instance of Invalidate.
-
#matches?(model) ⇒ Boolean
Run the test.
-
#with(*errors) ⇒ self
Set the with option.
Methods included from Base
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
#description ⇒ String
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_message ⇒ String
Explain why matches? fail
40 41 42 43 44 45 46 47 48 |
# File 'lib/rspec_model_validations/matchers/invalidate.rb', line 40 def 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? expected, @errors end |
#failure_message_when_negated ⇒ String
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 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? expected, @errors end |
#matches?(model) ⇒ Boolean
Run the test
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 |