Skip to content

Instantly share code, notes, and snippets.

@mizrael
Last active November 9, 2015 21:23
Show Gist options
  • Save mizrael/896b8f47fbf8b783b2f5 to your computer and use it in GitHub Desktop.
Save mizrael/896b8f47fbf8b783b2f5 to your computer and use it in GitHub Desktop.
how to test a controller that uses a promise to call a remote service
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