Skip to content

Instantly share code, notes, and snippets.

@andrew8088
Created July 27, 2015 21:59
Show Gist options
  • Save andrew8088/e4b3c4c1cc48a39f08a0 to your computer and use it in GitHub Desktop.
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.
'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);
});
});
}
});
});
};
'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();
});
});
@iamdtang
Copy link

I was thinking something like this:

'use strict';
var test = require('tape');
var sinon = require('sinon');
var rewire = require('rewire');
var User = require('./some-path/user'); // importing User into the test

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) {
       // stubbing it out here before calling users.createUser()
        sinon.stub(User, 'findOne').yields(null, {});

    users.createUser({ username: 'andrew', password: 'password'}, function (err, user) {
        t.equal(err.message, 'User "andrew" already exists');
        t.end();
    });
});

// ....

Rather than injecting a mock into the users module using rewire, you can import User into the test and stub out findOne directly before the module being tested (users in this case) gets called. I'd be curious to hear what others do though.

@andrew8088
Copy link
Author

@skaterdav85 Ah, I see; thanks!

@bevacqua
Copy link

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