Created
September 6, 2014 04:19
-
-
Save dannluciano/87a88436cfb2a5b3a342 to your computer and use it in GitHub Desktop.
The Best Way to Test the Validations on Models 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
class User < ActiveRecord::Base | |
validates :username, presence: :true | |
validates :password_digest, presence: :true | |
has_secure_password | |
end |
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Take a look at shoulda-matchers and in the validates_presence_of matcher =)