This is my test:
it('add.user() should POST to /users/, failure', function() {
mockBackend.expectPOST("/users/", {username:'u', password: 'p', email: 'e', location: 'loc'}).respond(400, {msg: "bad request"});
BaseService.add.user({username:'u', password: 'p', email: 'e', location: 'loc'});
mockBackend.flush();
});
afterEach(function() {
mockBackend.verifyNoOutstandingExpectation();
mockBackend.verifyNoOutstandingRequest();
});
And when I run this test, I get this error:
Chromium 53.0.2785 (Ubuntu 0.0.0) Factory: BaseService add.user() should POST to /users/, failure FAILED
[object Object] thrown
Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.3.15/$rootScope/inprog?p0=%24digest
at /home/user/Documents/ebdjango/ebdjangoapp/static/js/angular.js:63:12
at beginPhase (/home/user/Documents/ebdjango/ebdjangoapp/static/js/angular.js:14820:15)
at Scope.$digest (/home/user/Documents/ebdjango/ebdjangoapp/static/js/angular.js:14262:9)
at Function.$httpBackend.verifyNoOutstandingExpectation (node_modules/angular-mocks/angular-mocks.js:1557:38)
at Object.<anonymous> (tests/test_base.js:61:21)
This is BaseService.add.user()
:
self.add = {
user: function(user) {
return $http.post("/users/", user)
.then(function successHandler(response) {
return $http.post("/custom-api-auth/login", user)
}).then(function successHandler(response) {
$window.location.href = "/";
// if there are errors, rewrite the error messages
}).catch(function rejectHandler(errorResponse) {
for (prop in errorResponse.data) {
if (prop == "email") {
errorResponse.data[prop] = "Please enter a valid email address.";
} else if (prop == "username") {
errorResponse.data[prop] = "Username can only contain alphanumeric characters and '.'";
} else if (prop == "password") {
errorResponse.data[prop] = "Please enter a valid password";
}
}
throw errorResponse;
};
How do I prevent a $digest already in progress
error from occurring?
Edit: If I remove throw errorResponse;
, the test works But I need throw errorResponse;
there because I need to display the errors on the front end (which another controller takes care of.. BaseService.add.user().catch()
basically rewrites the errors which should be displayed on the front end).
Edit 2: When the error message says at Object.<anonymous> (tests/test_base.js:61:21)
it points to the line: mockBackend.verifyNoOutstandingExpectation();