Class: RiotRails::ActiveRecord::IsInvalidWhenMacro

Inherits:
AssertionMacro
  • Object
show all
Defined in:
lib/riot/active_record/assertion_macros.rb

Overview

An ActiveRecord assertion that expects to fail with invalid value for an attribute. Optionally, the error message can be provided as the exact string or as regular expression.

context "a User" do
  setup { User.new }
  topic.is_invalid_when :email, "fake", "is invalid"
  topic.is_invalid_when :email, "another fake", /invalid/
end

Instance Method Summary collapse

Methods inherited from AssertionMacro

#error_from_writing_value

Instance Method Details

#evaluate(actual, attribute, value, expected_error = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/riot/active_record/assertion_macros.rb', line 83

def evaluate(actual, attribute, value, expected_error=nil)
  actual_errors = Array(error_from_writing_value(actual, attribute, value))
  if actual_errors.empty?
    fail("expected #{attribute.inspect} to be invalid when value is #{value.inspect}")
  elsif expected_error && !has_error_message?(expected_error, actual_errors)
    message = "expected %s to be invalid with error message %s"
    fail(message % [attribute.inspect, expected_error.inspect])
  else
    pass("attribute #{attribute.inspect} is invalid")
  end
end