2017-10-31 3 views
0

Dies ist mein Code: start_end.jsWie in node.js Variable mit Sinon/Mokka verspotten

var glb_obj, test={}; 
var timer = 10; 

test.start_pool = function(cb) { 
    timer--; 
    if(timer < 1) { 
    glb_obj = {"close": true}; // setting object 
    cb(null, "hello world");  
    } else { 
    start_pool(cb); 
    } 
} 

test.end_pool = function(){ 
    if(glb_obj && glb_obj.close) { 
    console.log("closed"); 
    } 
} 

module.exports = test; 

Testfall:

var sinon = require('sinon'); 
var start_end = require('./start_end'); 

describe("start_end", function(){ 
    before(function() { 
     cb_spy = sinon.spy(); 
    }); 

    afterEach(function() { 
    cb_spy.reset(); 
    }); 

    it("start_pool()", function(done){ 
    // how to make timer variable < 1, so that if(timer < 1) will meet 
    start_end.start_pool(cb_spy); 
    sinon.assert.calledWith(cb_spy, null, "hello world"); 

    }); 
}); 

wie die Variable timer und glb_obj innerhalb der Funktionen ändern mit Sinon?

Antwort

0

Dies ist nicht möglich mit Sinon ab v4.1.2.

Sinon konzentriert sich auf das Testen von Verhalten durch Stubbing und Mocking - anstatt den internen Zustand zu ändern.


Wenn Sie die Werte der privaten Variablen ändern wollen, schauen Sie in so etwas wie rewire mit:

https://github.com/jhnns/rewire