I trying unit testing using Mocha/Chai using BDD style. Not sure where to start. Following is what the core code structure is. Assuming that getTemplates is an ajax call, how do I the different stages of an application. For ie before hitting sh.setTemplates() in init function, it has go through few conditions. How to unit test those conditions?
// Javascript
function myFunc(id){
var mf = this;
mf.id = id;
mf.init = function(){return init()};
mf.isIdValid = function(){return isIdValid()};
mf.setTemplates = function(){return setTemplates};
mf.getTemplates = function(){return getTemplates};
// Init
mf.init();
///////////////////////
function init(){
if(!id){
return false;
}
if(!sh.isIdValid()){
return false;
}
sh.setTemplates();
}
///////////////////////
function setTemplates(){
getTemplates(function(callBackTemplate){
if(!callbackTemplate){
return false;
}
// inject to dom
});
}
///////////////////////
// Async call
function getTemplates(){
return '<div>Test</div>';
}
}
///////////////////////////////////////
/////////////////////////////////////////
TEST JS Mocha/Chai
var expect = chai.expect;
describe('myFunc Class', function(){
var mf;
before(function(){
mf = new myFunc(1);
});
describe('mf.init()', function(){
it('should not result false', function(){
var result = mf.init();
expect(result).to.not.equal(false);
});
});
How to unit test those conditions?
Use the following process:
References
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.