jeudi 8 janvier 2015

How to avoid location.reload() in angular tests via jasmine, and test it

In one of my controllers I have a location.reload()


(For those who wants to know why, My login page does not load the entire app before the user logs in to reduce threshold on the server)


When I test the controller, it actually reloads the test in an infinite loop.


My questions is this:


How do I avoid the location.reload() from loading the test itself, and how do I even check whether it has been called? can I mock it somehow?


Here's the code for the controller:



(function() {
'use strict';

angular
.module('app.ctrls')
.controller('LoginController', LoginController);

LoginController.$inject = [ '$scope', 'AuthService' ];

function LoginController($scope, AuthService)
{
console.log('LoginController');
$scope.credantials = {
email: '',
password: '',
remember: false
}
$scope.login = function(credantials)
{
AuthService.login(credantials.email, credantials.password, credantials.remember || false).success(function(response) {
console.log(response);
if (response.email !== null)
{
$scope.logged = true;
console.log('login successful');
location.reload();
}
else
{
$scope.logged = false;
$scope.error = true;
}
}).error(function() {
console.log('failed to login');
});
};
}
})();


Here's the spec:



describe("successfully logged in", function() {
it("should reload the page", function() {
this.scope.credantials = { email: 'test@mail.com', password: '123', remember: false };
this.$httpBackend.expectPOST('/auth/login', this.scope.credantials).respond({first_name: "Bakayaru", email: "test@mail.com"});
this.$httpBackend.whenGET('/tpls/home.html').respond(200);
this.scope.login(this.scope.credantials);
this.$httpBackend.flush();
expect(this.scope).toBeDefined();
expect(this.scope.logged).toBeTruthy();
/* -------> TEST that location.reload() has been called HERE <--------- */
});
});

Aucun commentaire:

Enregistrer un commentaire