Class: ErgomentumRspec::Matchers::ActiveModel::BeInvalidFor

Inherits:
Object
  • Object
show all
Defined in:
lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb

Instance Method Summary collapse

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)
  @expected_error_message = expected_error_message
  self
end

#descriptionObject



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_messageObject



47
48
49
# File 'lib/ergomentum_rspec/matchers/active_model/be_invalid_for.rb', line 47

def failure_message
  @message
end

#matches?(validator) ⇒ Boolean

Returns:

  • (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.messages[:some_attribute].include?(@expected_error_message)
    true
  else
    @message = "expecting error message '#{@expected_error_message}' for invalid value '#{@value}' but got "\
               "'#{model.errors.messages[:some_attribute].join(', ')}'"
    false
  end
end