Last active
November 9, 2015 21:23
-
-
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
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