Created
July 27, 2015 21:59
-
-
Save andrew8088/e4b3c4c1cc48a39f08a0 to your computer and use it in GitHub Desktop.
Are these good unit tests? I've been lazy about doing good unit testing, but I'm trying to change that.
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
'use strict'; | |
var db = require('mongoose').connection; | |
var credential = require('credential'); | |
var User = require('./models/user'); // the mongoose model | |
exports.createUser = function createUser(userAttrs, callback) { | |
db.once('open', function () { | |
User.findOne({ username: userAttrs.username }, 'username', function (err, person) { | |
if (err) return callback(err); | |
if (person) { | |
return callback(new Error('User "' + userAttrs.username + '" already exists')); | |
} else { | |
credential.hash(userAttrs.password, function (err, hash) { | |
if (err) return callback(err); | |
userAttrs.password = hash; | |
var user = new User(userAttrs); | |
return user.save(function (err, user) { | |
if (err) return callback(err); | |
return callback(null, user); | |
}); | |
}); | |
} | |
}); | |
}); | |
}; |
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
'use strict'; | |
var test = require('tape'); | |
var sinon = require('sinon'); | |
var rewire = require('rewire'); | |
var users = rewire('../lib/users'); | |
users.__set__('db', { once: sinon.stub().yields() }); | |
test('Creating a user throws error if username is already taken', function (t) { | |
users.__set__('User', { findOne: sinon.stub().yields(null, {})}); | |
users.createUser({ username: 'andrew', password: 'password'}, function (err, user) { | |
t.equal(err.message, 'User "andrew" already exists'); | |
t.end(); | |
}); | |
}); | |
test('Creating a user returns the user if it does not exist', function (t) { | |
function UserStub(attrs) { | |
this.save = sinon.stub().yields(null, attrs); | |
} | |
UserStub.findOne = sinon.stub().yields(null, undefined); | |
users.__set__('User', UserStub); | |
users.__set__('credential', { hash: sinon.stub().yields(null, { hash: 'password' }) }); | |
users.createUser({ username: 'andrew', password: 'password' }, function (err, user) { | |
t.error(err); | |
t.equal(user.username, 'andrew'); | |
t.deepEqual(user.password.hash, 'password'); | |
t.end(); | |
}); | |
}); |
@skaterdav85 Ah, I see; thanks!
Strive not to mix conditionals with asynchronous callback nesting, as that's very prone to cause confusion to your future self, and maybe even your present self.
One way to do that is to use more guard clause style. E.g
if (foo) {
done(new Error('foo!')); return;
}
Instead of (in your code example)
if (foo) {
done(new Error('foo!')); return;
} else {
bar();
}
Other than that, your tests seem cool! Keep it up. Writing tests mostly comes down to practice!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was thinking something like this:
Rather than injecting a mock into the
users
module using rewire, you can importUser
into the test and stub outfindOne
directly before the module being tested (users
in this case) gets called. I'd be curious to hear what others do though.