简体   繁体   中英

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?

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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM