vendredi 16 septembre 2016

Chai assertion not working as expected using a promise

I have an issue with a unit test where the test stubs a service using sinon.js and returns a promise as follows:

Contrived code under test:

var CaseService = function() {
    this.getCaseStatus = function() {
        var deferred = Q.defer();
        deferred.resolve({data: 'some data'});
        return deferred.promise;
    }
};

module.exports = CaseService;

Unit Test

describe('Using the case service', function () {

    var caseService;

    before(function() {
        caseService = new CaseService();
    });

    describe('making an API call', function() {
        it('should be a success', function () {
            sinon.stub(caseService, 'getCaseStatus').returns(Q.when({data: ['yo'] }));
            caseService.getCaseStatus().then(function(result) {
                // This doesn't work here as the test passes, it fails 
                // correctly if I were to move it up two lines.
                expect(true).equal(false);
            })
        });
    });
});

At this point I receive a response back with the expected mocked data. However, the test passes when clearly it shouldn't. If I was to move:

expect(true).equal(false);

Up two lines then the test fails as expected. Why doesn't the "expect" work inside the then() function?

Any help would be appreciated.

Cheers,

Paul

Aucun commentaire:

Enregistrer un commentaire