Angular jasmine 2 $ q承诺不打电话。然后

时间:2016-12-05 10:12:26

标签: angularjs jasmine angular-promise

我真的很惊讶,不知道发生了什么,可能是什么解决方案......我正在使用这段代码进行测试。 .then永远不会被召唤!:

describe('PicturesSvc.updatePicturesList', function () {
    beforeEach(module('com.myapp'));

    it('download and update pictures list', function (done) {
        inject(function ($q, PicturesSvc, PicturesRemoteSvc) {
            console.log($q);
            var localPictures = [];
            var remotePictures = [
                {
                    id: 'A',
                    likes: 2,
                },
            ];

            $q(function (resolve, reject) {
                console.log('going to resolve promise...');
                resolve(remotePictures);
            })
                .then(function (val) {
                    console.log('Great! Promise has been resolved!', val);
                    done();
                })
                .catch(function (err) {
                    console.log('Something went wrong... :(', err);
                    done(err);
                });

            var deferred = $q.defer();
            deferred.resolve('Test again!');
            deferred.promise
                .then(function (result) {
                    console.log(result);
                });
    });

});

输出如下:

LOG: function Q(resolver) { ... }
LOG: 'going to resolve promise...'

  PicturesSvc.updatePicturesList
    ✖ download and update pictures list

Finished in 4.926 secs / 5.01 secs

SUMMARY:
✔ 0 tests completed
✖ 1 test failed

FAILED TESTS:
  PicturesSvc.updatePicturesList
    ✖ download and update pictures list
      PhantomJS 2.1.1 (Mac OS X 0.0.0)
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

1 个答案:

答案 0 :(得分:2)

在Angular中

resolve承诺你还需要致电$scope.$apply()

更多信息:https://docs.angularjs.org/guide/unit-testing#testing-promises