Skip to content

Instantly share code, notes, and snippets.

@dannluciano
Created September 6, 2014 04:19
Show Gist options
  • Save dannluciano/87a88436cfb2a5b3a342 to your computer and use it in GitHub Desktop.
Save dannluciano/87a88436cfb2a5b3a342 to your computer and use it in GitHub Desktop.
The Best Way to Test the Validations on Models in ActiveRecord
class User < ActiveRecord::Base
validates :username, presence: :true
validates :password_digest, presence: :true
has_secure_password
end
test "the user's valitations" do
assert @user._validators[:username].any? do |validator|
validator.is_kind_of? ActiveRecord::Validations::PresenceValidator
end
assert @user._validators[:password].any? do |validator|
validator.is_kind_of? ActiveRecord::Validations::PresenceValidator
end
assert @user._validators[:password].any? do |validator|
validator.is_kind_of? ActiveRecord::Validations::LengthValidator
end
end
test "the number of user's valitations" do
assert_equal 3, @user._validators.count
end
@maurogeorge
Copy link

Take a look at shoulda-matchers and in the validates_presence_of matcher =)

@caironoleto
Copy link

☝️ is the best choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment