class RSpec::BeValidWhenMatcher::BeValidWhen
Provides the implementation for ‘be_valid_when` matcher. Not intended to be instantiated directly. @api private
Attributes
Public Class Methods
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
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
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
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
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
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
@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
@see BeValidWhenMatcher#is_false
# File lib/rspec/be_valid_when_matcher.rb, line 140 def is_false is(false, 'false') end
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
@see BeValidWhenMatcher#is_true
# File lib/rspec/be_valid_when_matcher.rb, line 135 def is_true is(true, 'true') end
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
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
# File lib/rspec/be_valid_when_matcher.rb, line 157 def assert_model_existance raise ArgumentError, 'missing model' if @model.nil? end
# File lib/rspec/be_valid_when_matcher.rb, line 153 def assert_value_existence raise ArgumentError, 'missing value' unless @value_set end
# 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
# 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
# File lib/rspec/be_valid_when_matcher.rb, line 148 def value=(value) @value = value @value_set = true end
# 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