Created
August 5, 2011 03:11
-
-
Save cdemyanovich/1126847 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
class SomeRecord | |
attr_accessor :password | |
attr_reader :errors | |
def initialize(attrs = {}) | |
@password = attrs.delete(:password) | |
@errors = ActiveModel::Errors.new(self) | |
end | |
def read_attribute_for_validation(attr) | |
send(attr) | |
end | |
end | |
describe PasswordStrengthValidator do | |
subject { PasswordStrengthValidator.new(:attributes => [:password]) } | |
it "does not allow spaces" do | |
record = SomeRecord.new(:password => "a bcdefghi") | |
subject.validate(record) | |
record.errors[:password].should include("must not contain spaces") | |
end | |
it "requires at least one letter and one number" do | |
record = SomeRecord.new(:password => "abcdefghi") | |
subject.validate(record) | |
record.errors[:password].should include("must contain at least one letter and number") | |
end | |
it "recognizes valid password" do | |
record = SomeRecord.new(:password => "abcdefgh12") | |
subject.validate(record) | |
record.errors[:password].should be_blank | |
end | |
it "does not raise on nil password" do | |
record = SomeRecord.new(:password => nil) | |
lambda { subject.validate(record) }.should_not raise_error | |
end | |
it "does not invalidate on nil password" do | |
record = SomeRecord.new(:password => nil) | |
subject.validate(record) | |
record.errors[:password].should be_blank | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment