Why is my $ q deferred without being solved in Angular unit test?

I am trying to get my first Angular unit test to work. I am new to many concepts, but since I have already had to overcome many obstacles to go this far, I am posting the code. I saw similar questions, but none of them helped me solve this problem.

Here is the controller:

.controller('ThumbsCtrl', 
    ['$scope', 'backend', function($scope, backend) {
        backend.getImages()
            .then(function(images) {
                console.log("promise.then() called with "+images);
                $scope.images = images;
            }, function(err) {
               console.log(err);
            });
       }]
)

Here's the test:

describe('ThumbsCtrl', function() {
    var controller, rootScope;
    beforeEach(module('myApp.controllers'));
    beforeEach(inject(function($controller, $rootScope) {
        controller = $controller;
        rootScope = $rootScope;
    }));

    it('should contain images', inject(function($q) {
        var scope = rootScope.$new();
        var backend = {
            getImages: function() { 
                console.log("calling mock getImages()");
                var deferred = $q.defer();
                deferred.resolve('shite');
                return deferred.promise;
            }
        };
        var ctrl = controller('ThumbsCtrl', {
            $scope: scope,
            backend: backend
        });

        expect(scope.images).toContain('shite');
    }));
});

The test fails because $ scope.images is never set.

, ( backend.getImages() ), - , , .then() - ? , , .resolve(), , unit test . , ... ?

+2
2

, , :

scope.$digest();

(). , $q . Parse.com JS SDK, Promises , (). , $q , , ?

, Angular , unit test . AngularJS, (Mastering Web Dev AngularJS) , , unit test. - , ( QUnit). Karma, , jeez - , , .

+4

, , . , .

afterEach(function() {
    scope.$digest();
    mockBackend.flush();
    mockBackend.verifyNoOutstandingExpectation();
    mockBackend.verifyNoOutstandingRequest();

});

, $http- .

, , , ESPECIALLY, .

+2

All Articles