Angular jasmine 2 $q обещает не звонить .then

Я очень удивлен и не знаю, что происходит и какое может быть решение... Я тестирую этот фрагмент кода. .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.

person Miquel    schedule 05.12.2016    source источник
comment
Попробуйте заменить весь конструктор на $q.resolve(remotePictures).then(...)   -  person Muli Yulzary    schedule 05.12.2016


Ответы (1)


в Angular, чтобы resolve выполнить обещание, вам также нужно вызвать $scope.$apply()

подробнее здесь: https://docs.angularjs.org/guide/unit-testing#testing-promises

person pwolaq    schedule 05.12.2016
comment
Поскольку это служба, это также можно сделать с помощью $rootScope.$apply() :) docs. angularjs.org/api/ng/service/$q#testing - person Miquel; 05.12.2016
comment
Есть ли способ связать обещания? Теперь я понял, что второй .then в промисе не вызывается (первый .then находится в сервисе, поэтому нет смысла делать там $rootScope.$apply()). - person Miquel; 05.12.2016