Skip to content

Instantly share code, notes, and snippets.

@rehanift
Created March 24, 2012 19:50
Show Gist options
  • Save rehanift/2187265 to your computer and use it in GitHub Desktop.
Save rehanift/2187265 to your computer and use it in GitHub Desktop.
Using Mock Objects with Node.js and Jasmine
var events = require("events"),
util = require("util");
var UserAuthenticator = function(user_finder){
this.user_finder = user_finder;
};
util.inherits(UserAuthenticator, events.EventEmitter);
UserAuthenticator.make = function(dependencies){
var authenticator = new UserAuthenticator(dependencies.user_finder);
return authenticator;
};
UserAuthenticator.create = function(){};
UserAuthenticator.prototype.authenticate = function(email, password){
var self = this;
this.user_finder.on("user-found", function(user){
if (user.password == password) {
self.emit("authenticated");
} else {
self.emit("invalid-password");
}
});
this.user_finder.on("user-not-found", function(){
self.emit("user-not-found");
});
this.user_finder.find_by_email(email);
};
module.exports = UserAuthenticator;
var util = require("util"),
events = require("events");
var mock = {};
mock.UserFinder = (function(){
var klass = function(){};
util.inherits(klass, events.EventEmitter);
klass.prototype.find_by_email = function(email){};
return klass;
})();
var UserAuthenticator = require("./user-authenticator");
describe("UserAuthenticator", function(){
beforeEach(function(){
this.valid_user = {
email:"foo@example.com",
password:"bar"
};
this.user_finder = new mock.UserFinder();
this.authenticator = UserAuthenticator.make({
user_finder: this.user_finder
});
});
it("looks up the user by email", function(){
spyOn(this.user_finder, "find_by_email");
var email = "foo@example.com";
this.authenticator.authenticate(email, null);
expect(this.user_finder.find_by_email).toHaveBeenCalledWith(email);
});
it("emits an 'authenticated' event when valid credentials are passed", function(){
spyOn(this.authenticator,'emit');
this.authenticator.authenticate(this.valid_user.email, this.valid_user.password);
this.user_finder.emit("user-found",this.valid_user);
expect(this.authenticator.emit).toHaveBeenCalledWith("authenticated");
});
it("emits an 'user-not-found' event when an unknown email address is passed", function(){
spyOn(this.authenticator,'emit');
this.authenticator.authenticate(null, null);
this.user_finder.emit("user-not-found");
expect(this.authenticator.emit).toHaveBeenCalledWith("user-not-found");
});
it("emits an 'invalid-password' event when a the passed email and password do not match", function(){
spyOn(this.authenticator,'emit');
this.authenticator.authenticate(this.valid_user, null);
this.user_finder.emit("user-found", this.valid_user);
expect(this.authenticator.emit).toHaveBeenCalledWith("invalid-password");
});
});
/**
* Example implementation of UserFinder. It depends on a UserRepository object.
*/
var util = require("util"),
events = require("events");
var UserFinder = function(user_repository){
this.user_repository = user_repository;
};
UserFinder.make = function(dependencies){
return new UserFinder(dependencies.user_repository);
};
UserFinder.create = function(){
var users = new UserRepository();
return UserFinder.make({
user_repository: users
});
};
util.inherits(UserFinder, events.EventEmitter);
// This should be tested with an integration test (ie. it should hit a real database)
UserFinder.prototype.find_by_email = function(email){
var self = this;
this.repository.get({email: email}, function(user){
if (user) {
self.emit("user-found", user);
} else {
self.emit("user-not-found");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment