I have a problem on how to use jasmine spy objects to test controllers that 
depends on a service and I have been tussling with this issue for some time 
now as I am quite new to AngularJS based developments. Could you please 
suggest what changes should be made in order to implement this test 
correctly? 

In this controller I am trying to test the user login process and it calls 
loginRequest method in WebService to send out user credentials as a login 
message. A loginreply message is received by the controller.If login 
process is successful a URL redirection should happen.I have used jasmine 
spy objects to mock the WebService functionality.

I have tried to use jasmine spy objects to mock the WebService 
functionality but I seem to be doing something wrong which I have been 
unable to pinpoint.But I am currently having a problem evaluating the 
received message status and evaluating the URL redirection process based on 
that.

*AngularJS code*
———————

Apps.controller(‘loginCtrl’,['$scope','$location','WebService','$timeout',,'$log','$http',
function($scope,$location,WebService,$timeout,$log,$http)
{
//Hardcoded values for testing
$scope.username = '[email protected]';
$scope.password = 'viranga123';

$scope.login = function()
{
var login = {};
login.UserName = $scope.username;
login.Password = $scope.password;
WebService.loginRequest($location.host(),$location.port(),"Check","'?'",login);//
}

//Error in Unit testing this block
$scope.$on('LSuccess_msg',function(e,msg)
{
if(angular.equals(msg.Status,"LOGIN_SUCCESS"))
{
$timeout(function ()
{

window.location.href = "http://"+<--custom redirection URL is added here-->;}, 
10);

}
});

}]);


*Unit test used to test controller*
—————————————-
“use strict”;

describe(“Controller : Login controller”, function()
{
var $scope, ctrl, $location, $timeout, WebServiceMock;

beforeEach(module(‘Apps’, function($provide)
{
WebServiceMock = jasmine.createSpyObj(“WebService”, ["loginRequest"]);

WebServiceMock.loginRequest.andReturn({ UserID:’1′, SessionId:’1118430′, 
Status:’LOGIN_SUCCESS’, EMail:’[email protected]’, FirstName:’Viranga’});

$provide.value(“WebServiceMock”, WebServiceMock)

}));

beforeEach(inject(function($rootScope, $controller, $q, _$timeout_, 
_$location_, _WebServiceMock_)
{
$scope = $rootScope.$new();
$timeout = _$timeout_;
$location = _$location_;

ctrl = $controller(‘loginCtrl’,{
$scope: $scope,
$location: $location,
WebService: _WebServiceMock_,
$timeout: $timeout
});

$scope.$digest();
}));

it(‘should call loginRequest WebServiceMock’, function ()
{
$scope.username = ‘[email protected]’;
$scope.password = ‘viranga123′;
$scope.$digest();

$scope.login();
expect(WebServiceMock.loginRequest).toHaveBeenCalled();

//Validate recieved data
spyOn($scope, ‘$on’).andCallThrough();
expect(result.Status).toEqual(“LOGIN_SUCCESS”);//Error here
});
});
*Error*
——-
Chrome 36.0.1985 (Windows 7) Controller : Login controller should call is 
loginRequest on WebService FAILED
TypeError: Cannot read property ‘Status’ of undefined at null.


Could you please suggest what changes should be made in order to implement 
this test correctly ? 

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to