jeudi 29 octobre 2015

NodeJS unit test : sinonJS stub not working for method that had a spy

I'm stuck on a subset of functions that I'm trying to create stubs for. I've got 50+ tests that are passing, many of them with stubs. I'm following the same pattern for this subset of functions but for some reason they are behaving differently. The only difference between these functions and the successful stubs are a) the successful stubs are stubbed and restored in the beforeEach and afterEach functions whereas the broken stubs are being stubbed in the test where they run and b) the broken stubs previously had sinon spies attached to them, but I restore them before stubbing them so not sure if that's an issue.

Here's the code I'm testing. It's really simple:

    function gatherMeritEmployeesSeries(callback) {
    console.log(jobName + " : gatherMeritEmployees");
    gatherMeritEmployees(function (err) {
        if (err) {
            callback(err);
        } else {
            callback();
        }
    })
}

Here's the test I'm trying to run:

    describe('when gatherMeritEmployeesSeries function is called', function () {
    it('should call the callback', function () {
        t.gatherMeritEmployees.restore(); //removes the spy
        var gather = sinon.stub(t, 'gatherMeritEmployees');
        gather.callsArgWith(0, 'error');
        gatherMeritEmployeesSeries(cbspy);
        assert(cbspy.called);
        assert(cbspy.calledWith('error'));
        gather.restore();
    });
});

I'm using lcov-report to see my code coverage and it's showing this:

lcov report shows I'm not getting into the callback

When I run my tests, my report says this:

52 passing (176ms) 1 failing

1) transformEmployee.js when gatherMeritEmployeesSeries function is called should call the callback:

  AssertionError: false == true
  + expected - actual

  -false
  +true

So even though I should be passing in 'error' and calling the callback with 'error', my test is failing. Thanks in advance if you can help.

Aucun commentaire:

Enregistrer un commentaire