Class: ErgomentumRspec::Matchers::ActiveModel::BeInvalidFor
- Inherits:
-
Object
- Object
- ErgomentumRspec::Matchers::ActiveModel::BeInvalidFor
- Defined in:
- lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb
Instance Method Summary collapse
- #and_add_error(expected_error_message) ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
-
#initialize(value) ⇒ BeInvalidFor
constructor
A new instance of BeInvalidFor.
- #matches?(validator) ⇒ Boolean
Constructor Details
#initialize(value) ⇒ BeInvalidFor
Returns a new instance of BeInvalidFor.
11 12 13 |
# File 'lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb', line 11 def initialize(value) @value = value end |
Instance Method Details
#and_add_error(expected_error_message) ⇒ Object
42 43 44 45 |
# File 'lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb', line 42 def and_add_error() @expected_error_message = self end |
#description ⇒ Object
51 52 53 |
# File 'lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb', line 51 def description "'#{@value}' be a invalid value" end |
#failure_message ⇒ Object
47 48 49 |
# File 'lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb', line 47 def @message end |
#matches?(validator) ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb', line 15 def matches?(validator) model = Class.new do include ::ActiveModel::Model include ::ActiveModel::Attributes # ActiveModel sometimes needs a non nil class name. # Because we are using an anonymous class, which have no name by default, we have to override the name # method. def self.name "SomeClass" end attribute :some_attribute validates :some_attribute, validator end.new(some_attribute: @value) if model.valid? @message = "expecting '#{@value}' to be invalid" false elsif model.errors.[:some_attribute].include?(@expected_error_message) true else @message = "expecting error message '#{@expected_error_message}' for invalid value '#{@value}' but got "\ "'#{model.errors.[:some_attribute].join(', ')}'" false end end |