public
Created

DIP snippet 4

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# WARNING: pseudo code
 
class Authenticator
def initialize(user_repository = User)
@user_repository = user_repository
end
 
def authenticate(identifier, hashed_password)
@user_repository.find(:username => identifier, :password => hashed_password).present?
end
end
 
describe Authenticator
let(:repo) { double("user repo") }
let(:authenticator) { Authenticator.new(repo) }
 
context "with invalid credentials"
before { repo.stub(:find => nil) }
 
it "returns false" do
authenticator.authenticate("invalid", "invalid").should_not be
end
end
 
context "with valid credentials"
before { repo.stub(:find => double) }
 
it "returns true" do
authenticator.authenticate("valid", "valid").should be
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.