samedi 7 février 2015

Why my unit tests are failing when using $location and $route in angularjs?

I have all my unit tests failing after adding $location and $route to my controllers. I tried to mock them following a few answers found online but it doesn't seem to change anything.


Controllers:



myAppModule.controller('addUserCtrl', ['$scope', '$http', '$location',
function ($scope, $http, $location) {
$scope.addNewUser = function(user){
$http.post('url/users', user);
$location.path('/')
}
}
]);

myAppModule.controller('usersCtrl', ['$scope', '$http', '$route',
function ($scope, $http, $route) {
$http.get('url/users').success(function(data){
$scope.users = data;
})

$scope.delete = function(id){
$http.delete('url/users/' + id).success(function(){
$route.reload();
})
}
}
]);


Test setup for addUserCtrl:



describe('addUserCtrl controller', function(){

beforeEach(module('myAppModule'));

var scope, ctrl, $httpBackend;

var user = [
{
"username": "Zoltan",
"email": "Zoltan@Z.com"
}
]

beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectPOST('url/users', user).
respond(200, 'Done');
scope = $rootScope.$new();
ctrl = $controller('addUserCtrl', { $scope: scope });
}));

some test here

})


Test setup for usersCtrl:



describe('UsersCtrl', function(){

beforeEach(module('myAppModule'));

var scope, ctrl, $httpBackend;

var users = [
{
"username": "Zoltan",
"email": "Zoltan@Z.com",
"id": 1
},
{
"username": "Aria123",
"email": "aria@aria.com",
"id": 2
}
]

beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) {
$httpBackend = _$httpBackend_;
$httpBackend.expectGET('url/users').respond(users);
scope = $rootScope.$new();
ctrl = $controller('usersCtrl', { $scope:scope });
}));

some test here

})


I can write anything in the tests and they are failing with this error:



TypeError: undefined is not a function
at $LocationProvider.$get (/home/elena/projects/myAppInAngularJS/app/bower_components/angular/angular.js:11305:34)

Aucun commentaire:

Enregistrer un commentaire