Last active
November 9, 2015 21:23
how to test a controller that uses a promise to call a remote service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mockFooService, | |
sut; | |
describe('FooController tests', function () { | |
beforeEach(function($q, $controller){ | |
mockFooService = { | |
bar: function() { | |
return $q.reject({ data: { message: 'Error message' } }); | |
} | |
}; | |
var $scope = {}; | |
sut = $controller('FooController', { $scope: $scope, fooService: mockFooService }); | |
}); | |
it('callBar() should call onBarError() if an error is thrown', function () { | |
spyOn(sut, 'onBarError').andCallThrough(); | |
scope.callBar(); | |
scope.$apply(); // without the promise will not be evaluated | |
expect(sut.onBarError).toHaveBeenCalled(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment