test/model_test.rb in context_validations-0.0.3 vs test/model_test.rb in context_validations-0.1.0
- old
+ new
@@ -1,14 +1,13 @@
require 'test_helper'
-require 'active_model'
EmailFormat = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/
-class User
- include ActiveModel::Validations
+users_table = %{CREATE TABLE users (id INTEGER PRIMARY KEY, first_name TEXT, email TEXT);}
+ActiveRecord::Base.connection.execute(users_table)
+class User < ActiveRecord::Base
include ContextValidations::Model
- attr_accessor :first_name, :email
validates :first_name, :presence => true
validates :email, :format => EmailFormat
end
@@ -20,10 +19,10 @@
it 'ignores existing validations' do
@user.valid?.must_equal true
end
it 'accepts validations set onto the instance' do
- validations = [ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name]), ActiveModel::Validations::FormatValidator.new(:attributes => [:email], :with => EmailFormat)]
+ validations = [ActiveModel::Validations::PresenceValidator.new(:attributes => [:first_name]), ActiveModel::Validations::FormatValidator.new(:attributes => [:email], :with => EmailFormat), ActiveRecord::Validations::UniquenessValidator.new(:attributes => [:email])]
@user.validations = validations
@user.valid?.must_equal false
@user.errors.count.must_equal 2
end
end