繁体   English   中英

测试角度服务,异步返回Jasmine 2.0中的$ q Promise

[英]Test Angular Service that Returns a $q Promise in Jasmine 2.0 Asynchronously

我有一个测试承诺结果的规范。

承诺解决,但thencatch ,并finally处理似乎永远火,并调用done();

因此,规范超时并失败(请参阅Chrome Developer Console的屏幕截图)。

Jasmine套件和规格

describe("promise suite", function () {
        var someMethod = function() {
            var deferred = $q.defer();
            setTimeout(function() {
                console.log('All done calling resolve');
                deferred.resolve('all done');
            }, 500);
            return deferred.promise;
        }
        iit("promise spec", function (done) {
            someMethod()
                .then(function(message) {
                    console.log('promise method resolved, running expectation');
                    expect(message).toBe('all done');
                    done();
                })
                .catch(function(error) {
                    console.log('promise was rejected ', error);
                    done();
                })
                .finally(function() {
                    console.log('calling done from finally');
                    done();
                });
        });
    });

在此输入图像描述

$ q promises在下一个摘要中得到解决...所以你需要触发一个摘要:

$rootScope.$digest();

尝试使用plunkr中的代码并且它正常工作,请参阅控制台输出。

由于示例中没有app引导程序,我使用https://docs.angularjs.org/api/ng/function/angular.injectorng模块来获取$q服务

http://plnkr.co/edit/pIZ8LVtlyERXVxQB9HI2?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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