How do you mock $http so that when you call a specific URL the .error function is called?
beforeEach(angular.mock.inject(function ($httpBackend) {
$httpBackend.when('GET', '/ErrorReturningURL').respond(/* report a 404 */);
}));
In my unit test I will be watching for a certain function call.
You can use the alternate form of the .respond function on the value returned by when:
beforeEach(angular.mock.inject(function ($httpBackend) {
$httpBackend.when('GET', '/ErrorReturningURL').respond(404, '');
}));
I am using the syntax function([status,] data[, headers]) for the respond function. Hence, I need to explicitly pass in the second argument data to ensure that the first argument is interpreted as status.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With