Skip to content

Instantly share code, notes, and snippets.

@cdemyanovich
Created August 5, 2011 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdemyanovich/1126847 to your computer and use it in GitHub Desktop.
Save cdemyanovich/1126847 to your computer and use it in GitHub Desktop.
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