Class: RSpec::BeValidWhenMatcher::BeValidWhen Private

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/be_valid_when_matcher.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Provides the implementation for ‘be_valid_when` matcher. Not intended to be instantiated directly.

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ BeValidWhen

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of matcher.

Parameters:

  • field (Symbol)

    field name to use.

Raises:

  • ArgumentError if field name is not a symbol.



16
17
18
19
20
21
22
23
24
25
# File 'lib/rspec/be_valid_when_matcher.rb', line 16

def initialize(field)
  unless field.instance_of? Symbol
    raise ArgumentError, "field name should be symbol (#{field.inspect})"
  end

  @field     = field
  @value_set = false
  @value     = nil
  @model     = nil
end

Instance Method Details

#descriptionString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Used to generate the example’s doc string in one-liner syntax.

Returns:

  • (String)

    short description of what is expected.



73
74
75
76
77
# File 'lib/rspec/be_valid_when_matcher.rb', line 73

def description
  assert_value_existence

  "be valid when #{format_message}"
end

#diffable?FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates that this matcher doesn’t provide actual and expected attributes.

Returns:

  • (FalseClass)


81
82
83
# File 'lib/rspec/be_valid_when_matcher.rb', line 81

def diffable?
  false
end

#does_not_match?(model) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Passes if given ‘model` instance is invalid.

More specifically if it does have ‘errors` on specified `field` after setting it’s ‘value` and validating it. Does not take into account other fields.

Parameters:

  • model (Object)

    an Object implementing ‘ActiveModel::Validations`.

Returns:

  • (Boolean)

    ‘true` if there are errors on `field`, `false` otherwise.



48
49
50
51
# File 'lib/rspec/be_valid_when_matcher.rb', line 48

def does_not_match?(model)
  setup_model model
  !@model.errors[@field].empty?
end

#failure_messageString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called when #matches? returns false.

Returns:

  • (String)

    explaining what was expected.



55
56
57
58
59
60
# File 'lib/rspec/be_valid_when_matcher.rb', line 55

def failure_message
  assert_value_existence
  assert_model_existance

  "expected #{@model.inspect} to be valid when #{format_message}"
end

#failure_message_when_negatedString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called when #does_not_match? returns false.

Returns:

  • (String)

    explaining what was expected.



64
65
66
67
68
69
# File 'lib/rspec/be_valid_when_matcher.rb', line 64

def failure_message_when_negated
  assert_value_existence
  assert_model_existance

  "expected #{@model.inspect} not to be valid when #{format_message}"
end

#is(*args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rspec/be_valid_when_matcher.rb', line 92

def is(*args)
  number_of_arguments = args.size

  if number_of_arguments > 2 || number_of_arguments == 0
    raise ArgumentError, "wrong number of arguments (#{number_of_arguments}
      insted of 1 or 2)"
  else
    self.value = args.shift
    @message = args.first
  end

  self
end

#is_falseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



140
141
142
# File 'lib/rspec/be_valid_when_matcher.rb', line 140

def is_false
  is(false, 'false')
end

#is_not_presentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Style/PredicateName



108
109
110
# File 'lib/rspec/be_valid_when_matcher.rb', line 108

def is_not_present
  is(nil, 'not present')
end

#is_trueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



135
136
137
# File 'lib/rspec/be_valid_when_matcher.rb', line 135

def is_true
  is(true, 'true')
end

#matches?(model) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Passes if given ‘model` instance is valid.

More specifically if it doesn’t have any ‘errors` on specified `field` after setting it’s ‘value` and validating it. Does not take into account other fields and the validity of the whole object.

Parameters:

  • model (Object)

    an Object implementing ‘ActiveModel::Validations`.

Returns:

  • (Boolean)

    ‘true` if there are no errors on `field`, `false` otherwise.



35
36
37
38
# File 'lib/rspec/be_valid_when_matcher.rb', line 35

def matches?(model)
  setup_model model
  @model.errors[@field].empty?
end

#supports_block_expectations?FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indicates that this matcher cannot be used in a block expectation expression.

Returns:

  • (FalseClass)


87
88
89
# File 'lib/rspec/be_valid_when_matcher.rb', line 87

def supports_block_expectations?
  false
end