Skip to content

Instantly share code, notes, and snippets.

@hugozap
Created January 17, 2013 01:08
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 hugozap/4552618 to your computer and use it in GitHub Desktop.
Save hugozap/4552618 to your computer and use it in GitHub Desktop.
Should and Mocha snippets
var mocha = require('mocha');
var should = require('should');
//Our fake validation component
var Validator = {
validateName:function(name){
if (typeof name !== 'string')
return false;
if(name.length==0)
return false;
return true;
}
}
//The test suite
describe('Validator',function(){
var testName,valid;
it('should not accept invalid types',function(done){
testName = 123;
valid = Validator.validateName(testName);
valid.should.be.false;
done();
});
it('shoud not accept empty string',function(done){
testName = '';
valid = Validator.validateName(testName);
valid.should.be.false;
done();
});
it('should accept valid strings',function(done){
testName = 'Valid Name';
valid = Validator.validateName(testName);
valid.should.be.true;
done();
})
})
var should = require('should');
var testobj = {
name:"Mary",
age:29,
company:"none",
email:"mary@mary.com",
properties:[
{
"name":"Lake house",
"value":100000
}
]
};
//This should pass (no exception will be thrown)
testobj.should.have.property("name");
testobj.age.should.be.above(20);
testobj.company.should.be.a("string");
testobj.properties.should.not.be.empty;
//This will throw an exception
testobj.properties.length.should.be.within(10,20);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment