mercredi 21 août 2019

How to spy on a function that is also referenced by a property

Given this code:

function getAnimal(type, color) {
    console.log('blub'); // this is triggered when executing the test
}
this.getAnimal = getAnimal;

and this testing code:

describe('getAnimal()', function() {
    it('should get an animal based on type and color', function() {
        spyOn(this.AnimalService, 'getAnimal');
        this.AnimalService.getAnimal();

        expect(this.AnimalService.getAnimal).toHaveBeenCalled();
    });
});

But I get this error message: Expected spy function(){return i} to have been called.

So I guess only the 'this.getAnimal = getAnimal' is spied, but not the function. How can I fix this?

Aucun commentaire:

Enregistrer un commentaire