I'm currently working on properly injecting the $window service into my Angular controller, and then testing to ensure that it redirects correctly. However, I'm running into an issue where I'm getting an error message that says
undefined is not a constructor (evaluating 'expect(window.location.href).toEqual('/profile')')
. Here's a snippet of my Angular controller code:
login.submitLogin = function(){
LoginFactory.loginUser(login.dataset)
.then(function(response){
$window.location.href = '/profile'
},function(response) {
login.errorMessage = response.data.message;
});
};
My unit test in Karma looks like this:
describe('Login Controller', function() {
var controller, window;
beforeEach(angular.mock.module('app'));
beforeEach(inject(function(_$controller_, _$window_){
window = _$window_;
controller = _$controller_('LoginCtrl',window);
}));
describe('Login', function() {
it('expects controller to be defined', function(){
expect(controller).to.be.defined;
});
it('expects to be redirected after login', function() {
controller.dataset.username = 'username';
controller.dataset.password = 'password';
controller.submitLogin();
expect(window.location.href).toEqual('/profile');
});
});
});