3

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://0.0.0.0:8000/api/auth/login/', {
            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://0.0.0.0:8000/api/auth/login/").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();
        })
    })
})
Peter Graham
  • 2,467
  • 2
  • 24
  • 29

0 Answers0