0

I'm really surprised and don't know what's going on and what could be the solution... I'm testing with this piece of code. .then is never being called!:

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

});

The output is the following one:

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.
Miquel
  • 8,339
  • 11
  • 59
  • 82

1 Answers1

2

in Angular in order to resolve a promise you also need to call $scope.$apply()

more here: https://docs.angularjs.org/guide/unit-testing#testing-promises

pwolaq
  • 6,343
  • 19
  • 45
  • As it is a service, it can also be done with `$rootScope.$apply()` :) https://docs.angularjs.org/api/ng/service/$q#testing – Miquel Dec 05 '16 at 10:30
  • Is there any way to chain promises? Now I've realized that second `.then` in a promise is not being called (first `.then` is in the service so it makes no sense to make a `$rootScope.$apply()` there). – Miquel Dec 05 '16 at 10:42