I'm currently working on testing one of my controllers, but I've encountered an issue. The $LoginController
object I created always ends up being empty, and all my methods are present in the scope object instead. When attempting to test the hasError
function, I receive the following message:
TypeError: 'undefined' is not an object (evaluating '$route.current.params')
Below is the code snippet for my test case:
describe('login', function () {
var $LoginController = null, $scope, $route, $location, $httpBackend;
beforeEach(module('common.base64'));
beforeEach(module('myapp.user'));
beforeEach(inject(function ($injector, $controller, $rootScope,
_$location_, _$timeout_, _$route_, _Base64_)
{
$httpBackend = $injector.get('$httpBackend');
$scope = $rootScope.$new();
$route = _$route_;
$location = _$location_;
$LoginController = $controller('LoginController', {
$scope : $scope, $http: $httpBackend, $location: _$location_,
$timeout: _$timeout_, $route: _$route_, Base64: _Base64_}
);
console.log($LoginController); // <-- Log: {}
console.log($scope); // <-- Log: { <lots of stuff> }
}));
describe('LoginController', function() {
it('should have been injected', inject(function () {
expect($LoginController).toBeTruthy();
}
));
});
describe('hasError', function () {
it('should return error', inject(function () {
$location.url('/login?loginError=true');
expect($scope.hasError).toBeTruthy();
// ^-- hasError is defined!
expect($LoginController.hasError).toBeTruthy();
// ^-- hasError is undefined!
}
));
}
);
}
);
The hasError
function within my LoginController looks like this:
$scope.hasError = function () {
if($route.current.params) {
if ($route.current.params['loginError']) {
return "error";
} else {
return "";
}
}
return "";
};
This function is then injected into an element's class attribute. Can you please help me identify whether there is an issue in my controller or in my test setup?