Skip to content

Instantly share code, notes, and snippets.

@jdudek
Created November 24, 2010 21:50
Show Gist options
  • Save jdudek/714497 to your computer and use it in GitHub Desktop.
Save jdudek/714497 to your computer and use it in GitHub Desktop.
Test validations in ActiveRecord
require 'spec_helper'
module ActiveModel
class Errors
def error_names
@_error_names ||= { }
end
def add_with_save_names(attribute, message = nil, options = {})
message ||= :invalid
if message.is_a?(Proc)
message = message.call
end
error_names[attribute] ||= []
error_names[attribute] << message
add_without_save_names(attribute, message, options)
end
alias_method_chain :add, :save_names
end
end
describe User do
it "should not allow to short name" do
u = User.new(:name => "a")
u.valid?
puts u.errors.error_names
u.errors.error_names[:name].should include(:too_short)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment