How do I organize code to unit test BDD using Mocha Chai?

I am testing unit testing using Mocha / Chai using BDD style. Not sure where to start. Below is the structure of the main code. Assuming getTemplates is an ajax call how can I use different stages of the application. For example, before removing sh.setTemplates () in init function, it passed several conditions. How to unit test these 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);
    });


});

      

+3


source to share


1 answer


How to unit test these conditions?

Use the following process:

  • Create a branch function
  • Put the statement in a branch function
  • Use Variant as Argument
  • Call it with a truthful meaning
  • Call again with a false value


Links

0


source







All Articles