Created
January 17, 2013 01:08
-
-
Save hugozap/4552618 to your computer and use it in GitHub Desktop.
Should and Mocha snippets
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
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(); | |
}) | |
}) |
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
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