var $controllerConstructor, ctr, mockSuperheroData, scope, deferred, q; describe('main controller', function() { var $controllerConstructor, ctr, mockSuperheroData, scope, deferred, q, rootScope; beforeEach(inject(function($controller, $rootScope, $q) { scope = $rootScope.$new(); $controllerConstructor = $controller; q = $q; rootScope = $rootScope; mockSuperheroData = { getSuperheroes: function() { deferred = q.defer(); return deferred.promise; } }; ctr = $controllerConstructor('MainCtrl', {$scope: scope, $location: {}, superheroService: mockSuperheroData, keys: {}}); })); it('should set the result of getResource to scope.heroes', function() { scope.getHeroes(); deferred.resolve(100); rootScope.$apply(); expect(scope.heroes).toBe(100); }); });
It should be noted that since $ q is integrated with $ rootScope, you not only need to allow the pending object, but you also need to propagate the changes by invoking $ rootScope. $ apply.
I wrote a blog about the mock angular promises in projectpoppycock
and I wrote a working example with plunkr tests
I really like the way you enter the service layout as a controller dependency using the $ controller service, I may have to reconsider my way of doing this. Your way is better :) Thank you!
source share