lundi 2 février 2015

AngularJS: $location path not defined: Unit Testing. Karma-Jasmine

I am having a great difficulty in finding out what is wrong with this code snippet. I have a login page, after sucessfull login it redirects me to the protected resource page. Everything is working fine. expect the UNIT TESTING part.


Here is myTest





ddescribe("Login Controller", function () {
var scope, controller, location, http, ControllerService, state;
beforeEach(module('myApp'));
beforeEach(inject(function ($httpBackend, $rootScope, $controller, $location) {
scope= $rootScope.$new();
http= $httpBackend;
location = $location
ControllerService= $controller;
}));
beforeEach(inject(function ($injector) {
location = $injector.get('$location');
}));

iit("should do mock authentication", function () {
http.expectPOST('http://ift.tt/1LDhR7G').respond('True');
http.expectPOST('http://ift.tt/1wW3UH2').respond('4nd8n129dd01m1md8');
Newcontroller = ControllerService("simpleController", {
$location:location, $scope:scope,
tokenfactory:tokenfactory});
scope.$digest();
scope.login();
http.flush();
expect(scope.somedata).toBe('True');

});
)};



And here is my login() function





var app = angular.module('myApp',['ui.bootstrap','ui.router','ngCookies', 'UserValidation','restangular']);

app.controller ('simpleController',function($scope ,$http, $location, tokenfactory,loginusercheck, usernamefactory,Restangular){

$scope.login = function(){
// some login function
//after post to login endpoint
//change the page state to something else

$location.path('list'); // where list is the url to protected page


};
})



Now when i run this using karma-jasmine test runner, I get error



typeError: undefined is not a function at $LocationProvider.$get


If I comment out the $location.path('list') and dont inject the $location in my test case = everything works fine. What am i doing wrong. I cant seem to inject $location. is there anyother wat to inject $location?


Aucun commentaire:

Enregistrer un commentaire