dimanche 31 janvier 2016

Error: spyOn could not find an object to spy upon for login()

I am trying on unit test for login using karma and jasmine. I got the following error when I test my code using karma start unit-tests.conf.js.

PhantomJS 1.9.8 (Mac OS X 0.0.0) LoginController doLoginAction should call login method on UserService FAILED

Error: spyOn could not find an object to spy upon for login()

Here I am including my login.controller.tests.js page.

describe('LoginController', function() {

var scope, controller, userServiceMock, stateMock;

beforeEach(module('user.controllers'));

beforeEach(inject(function($rootScope, $controller) {
    scope = $rootScope.$new();
    controller = $controller('LoginController',{
        $scope: scope,
        $state: stateMock,
        UserService: userServiceMock
    });
}));

beforeEach(function() {
    scope.doLoginAction = jasmine.createSpy('doLoginAction')
});

describe('doLoginAction',function(){
    it('should call odLoginAction method on LoginController', function(){
        scope.doLoginAction();
    });

    it('should call login method on UserService', function(){            
        spyOn(userServiceMock,'login');
        expect(userServiceMock.login).toHaveBeenCalledWith({username:'riju@gmail.com',password:'riju'});

    });
});

});

doLoginAction function in my controller page.

$scope.doLoginAction = function () {
            UserService.login($scope.creds.username, $scope.creds.password)
                .then(function (_response) {

                    alert("login success " + _response.attributes.username);

                    // transition to next state
                    $state.go('tab.list');

                }, function (_error) {
                    alert("error logging in " + _error.message);
                })
        };

What I am doing wrong, please help me. thanks

Aucun commentaire:

Enregistrer un commentaire