Created
November 24, 2010 21:50
-
-
Save jdudek/714497 to your computer and use it in GitHub Desktop.
Test validations in ActiveRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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