1
votes

It's my first test on Javacript with Mocha/Sinon/Chai And I don't know if it's possible to do this :

var obj = {
    first : function () {
        console.log('make job 1');
    }
};

var objManager = function() {
    $(document).on('event1', obj.first);
};

new objManager();

var spy = sinon.spy(obj, 'first');

describe('Test', function () {

    it('My first test', function () {
        $(document).trigger('event1');
        spy.should.not.have.been.called;
    });
});

My spy isn't called and don't understand why... My function "obj.first" has printed "make job 1".

if I modify my test by :

it('My first test', function () {
    obj.first();
    spy.should.not.have.been.called;
});

My spy is called. So my question is : How make sinon spy work with a event ?

1

1 Answers

1
votes

The problem is that you first bind the function to the event and then replace the function in obj with the spy. Doing this will not have any effect on the function you have bound to the event cause this is still the original function.

Do test this you have to create the spy before instantiate your objManager.