mardi 9 août 2016

How to test `catch` and `then` with sinon js?

I have been trying to stub and mock my function to be able to test my function

SetExample.createCandyBox = function(config) {
    this.getCandies(config.candyUrl)
    .catch(() => {
        return {};
    })
    .then((candies) => {
        Advertisements.pushCandyBox(config, candies);
    });
};

I want to test a scenario when the config.candyUrl is incorrect(404, etc) with something like:

it('should return to an empty array when url is incorrect', sinon.test(function() {
    // Fixture is an element I created for testing. 
    var configStub = SetExample.getCandyConfig(fixture);
    var createCandyBoxMock = sinon.mock(config);

    createCandyBoxMock.expects('catch');

    SetExample. createCandyBox(configStub);
}));

When I do this, term is error => Can't find variable: config. What did I do wrong? can someone help and explain? I am new to Sinon :( Thx in advance!

Aucun commentaire:

Enregistrer un commentaire