Skip to content

Instantly share code, notes, and snippets.

@maldo
Created March 21, 2013 11:05
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 maldo/5212253 to your computer and use it in GitHub Desktop.
Save maldo/5212253 to your computer and use it in GitHub Desktop.
Example of mocha testing for https://github.com/justin-john/node-user-management/blob/master/routes/users.js#L107-L146, you can add more testing in error conditions
var should = require('chai').should();
var superagent = require('superagent');
var agent;
describe('testing user', function(){
before(function (done) {
agent = superagent.agent();
// some other operation you need
// like drop your collection for testing o adding something
});
it('should not register a user with a weak password', function (done){
agent
.post(route.signup)
.send({username: 'This is a valid name'})
.send({email : 'my@example.com'})
.send({pwd : ''})
.end(function (req, res) {
res.should.have.property('text').that.contain('The password can\'t be empty and 6 - 16 characters required');
done();
});
});
it('registers a new user', function (done) {
agent
.post(route.signup)
.send({username: 'My_name'})
.send({email : 'my@example.com'})
.send({pwd : '123456a'})
.end(function (req, res) {
res.should.have.property('statusCode').that.equals(200);
res.should.have.property('text').that.contain(username);
done();
});
});
it('fails to register a new user with a registered email', function (done) {
agent
.post(route.signup)
.send({username: 'My_name'})
.send({email : 'my@example.com'})
.send({pwd : '123456a'})
.end(function (req, res) {
res.should.have.property('statusCode').that.equals(200);
res.should.have.property('text').that.contain('This email is already taken!');
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment