mardi 3 février 2015

Angular Unit Testing HTTP

I'm trying to unit test my login function and I'm able to get the function to run but it does not process the $http request. I know this because it logs 'run function' in the console but it does not log 'success' or 'error'. Anyone know the error? Thanks!


homeController



$scope.login = function() {
console.log('run function')
$http.post(
'http://ift.tt/1HWWL4K', {
email: $scope.email,
password: $scope.password
}
)
.success(function(data) {
console.log("success");
})
.error(function(error) {
console.log("error");
});
};


homeControllerTest



describe('homeController', function() {
beforeEach(module('simulatorApp'));

var $controller;
beforeEach(inject(function (_$controller_) {
$controller = _$controller_;
}));

var $scope = {};
beforeEach(inject(function ($controller, $httpBackend) {
$httpBackend.whenPOST("http://ift.tt/1HWWL4K").respond('response');
$controller('homeController', {
$scope: $scope
});
$scope.login()
}));

describe('Authentication', function () {

it('Authenticates User', function () {
$scope.email = 'testuser1@j.com';
$scope.password = 'password';
$scope.login();
})
})
})

Aucun commentaire:

Enregistrer un commentaire