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 . , ... ?