Class: RiotRails::ActiveRecord::ValidatesUniquenessOfMacro

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

Overview

An ActiveRecord assertion that expects to fail with an attribute is not valid for record because the value of the attribute is not unique. Requires the topic of the context to be a created record; one that returns false for a call to new_record?.

context "a User" do
 setup { User.create(:email => "[email protected]", ... ) }
 topic.validates_uniqueness_of :email
end

Instance Method Summary collapse

Methods inherited from AssertionMacro

#error_from_writing_value

Instance Method Details

#evaluate(actual, attribute) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/riot/active_record/assertion_macros.rb', line 112

def evaluate(actual, attribute)
  actual_record = actual
  if actual_record.new_record?
    fail("topic is not a new record when testing uniqueness of #{attribute.inspect}")
  else
    copied_model = actual_record.class.new
    actual_record.attributes.each do |dup_attribute, dup_value|
      copied_model.write_attribute(dup_attribute, dup_value)
    end
    copied_value = actual_record.read_attribute(attribute)
    if error_from_writing_value(copied_model, attribute, copied_value)
      pass("#{attribute.inspect} is unique")
    else
      fail("expected to fail because #{attribute.inspect} is not unique")
    end
  end
end