class RSpec::BeValidWhenMatcher::BeValidWhen

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

Attributes

message[W]

Public Class Methods

new(field) click to toggle source

Returns a new instance of matcher. @param field (Symbol) field name to use. @raise ArgumentError if field name is not a symbol.

# 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

Public Instance Methods

description() click to toggle source

Used to generate the example’s doc string in one-liner syntax. @return [String] short description of what is expected.

# File lib/rspec/be_valid_when_matcher.rb, line 73
def description
  assert_value_existence

  "be valid when #{format_message}"
end
diffable?() click to toggle source

Indicates that this matcher doesn’t provide actual and expected attributes. @return [FalseClass]

# File lib/rspec/be_valid_when_matcher.rb, line 81
def diffable?
  false
end
does_not_match?(model) click to toggle source

Passes if given ‘model` instance is invalid.

More specifically if it does have {api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-errors ‘errors`} on specified `field` after setting it’s ‘value` and validating it. Does not take into account other fields. @param model [Object] an Object implementing `ActiveModel::Validations`. @return [Boolean] `true` if there are errors on `field`, `false` otherwise.

# File lib/rspec/be_valid_when_matcher.rb, line 48
def does_not_match?(model)
  setup_model model
  !@model.errors[@field].empty?
end
failure_message() click to toggle source

Called when {#matches?} returns false. @return [String] explaining what was expected.

# 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_negated() click to toggle source

Called when {#does_not_match?} returns false. @return [String] explaining what was expected.

# 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) click to toggle source

@see BeValidWhenMatcher#is

# 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_false() click to toggle source

@see BeValidWhenMatcher#is_false

# File lib/rspec/be_valid_when_matcher.rb, line 140
def is_false
  is(false, 'false')
end
is_not_present() click to toggle source

rubocop:disable Style/PredicateName @see BeValidWhenMatcher#is_not_present

# File lib/rspec/be_valid_when_matcher.rb, line 108
def is_not_present
  is(nil, 'not present')
end
is_true() click to toggle source

@see BeValidWhenMatcher#is_true

# File lib/rspec/be_valid_when_matcher.rb, line 135
def is_true
  is(true, 'true')
end
matches?(model) click to toggle source

Passes if given ‘model` instance is valid.

More specifically if it doesn’t have any {api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-errors ‘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. @param model [Object] an Object implementing `ActiveModel::Validations`. @return [Boolean] `true` if there are no errors on `field`, `false` otherwise.

# File lib/rspec/be_valid_when_matcher.rb, line 35
def matches?(model)
  setup_model model
  @model.errors[@field].empty?
end
supports_block_expectations?() click to toggle source

Indicates that this matcher cannot be used in a block expectation expression. @return [FalseClass]

# File lib/rspec/be_valid_when_matcher.rb, line 87
def supports_block_expectations?
  false
end

Private Instance Methods

assert_model_existance() click to toggle source
# File lib/rspec/be_valid_when_matcher.rb, line 157
def assert_model_existance
  raise ArgumentError, 'missing model' if @model.nil?
end
assert_value_existence() click to toggle source
# File lib/rspec/be_valid_when_matcher.rb, line 153
def assert_value_existence
  raise ArgumentError, 'missing value' unless @value_set
end
format_message() click to toggle source
# File lib/rspec/be_valid_when_matcher.rb, line 169
def format_message
  value = value_to_string
  message = @message.nil? ? "is #{value}" : "is #{@message} (#{value})"
  "##{@field} #{message}"
end
setup_model(model) click to toggle source
# File lib/rspec/be_valid_when_matcher.rb, line 161
def setup_model(model)
  assert_value_existence

  @model = model
  @model.send "#{@field}=", @value
  @model.validate
end
value=(value) click to toggle source
# File lib/rspec/be_valid_when_matcher.rb, line 148
def value=(value)
  @value = value
  @value_set = true
end
value_to_string() click to toggle source
# File lib/rspec/be_valid_when_matcher.rb, line 175
def value_to_string
  if [Complex, Rational, BigDecimal].any? { |type| @value.is_a? type }
    @value.to_s
  else
    @value.inspect
  end
end