Search code examples
javascriptangularjsjasminejasmine2.0

Promise not expecting a resolve or a reject


To test one of my AngularJs Service I was writing to Unit tests.

Here is a sample code I've came up with :

it('', function(done) {
    aDocument.retrieveServiceFile(extractedFileFeature)
        .then(function() {
            expect(true).toBeFalsy();
        }, function() {
            expect(true).toBeTruthy();
        });
    $rootScope.$digest();
    done();
}

I just want to check if the defer has been resolved or reject. I don't find it really satisfying as the expect isn't really explicit.

Is there a better way than this to check whether the deferred has been rejected or resolved?


Solution

  • After some advice, I went for the following solution which is pretty easy to read and understand !

    promise.then(function() {
        shouldHaveBeenHere = false;
    }, function() {
        shouldHaveBeenHere = true;
    }).finally(function() {
        expect(shouldHaveBeenHere).toBeTruthy();
    });