vendredi 23 janvier 2015

How to assign value to variables in factory and test a method using assigned value in AngularJS?

I have following factory. I have removed init method for the sake of brevity



var AppController = angular.module("AppController");
AppController.factory('TeamFactory', ['$http', '$q',
function($http, $q) {

var teamsConfig = {};
return{
init: function() {
$http.get('navigation/TeamsConfiguration.json').success(function(data) {
teamsConfig = data;
}).error(function(msg, code) {
deferred.reject(msg);
});

return deferred.promise;
},

getCaptainName: function(team) {
return teamsConfig[team].captainName;
}

};


My Jasmine code is as below



describe('Test TeamCaptain',function(){
beforeEach(module('AppController'));
var ctrl,scope,http,route, routeParams,location,q,svc,factory;
beforeEach(inject(function(TeamFactory,$rootScope){
scope = $rootScope.$new();
TeamFactory.teams= {
"India":
{
"captain": "Dhoni",
"batsman": "Kohli"
},
"Australia":
{
"captain": "Clarke",
"batsman": "Watson"
}
};

factory = TeamFactory;
}));
it('should get the captain of the team.',function(){
var captain= '';
var team = 'India';
captain = factory.getCaptainName(team);
expect(captain).toEqual('Dhoni');
});
}));


While running tests, I'm getting error --TypeError: 'undefined' is not an object evaluating 'teamsConfig[team].captainName'


I have changed the code from original, it may have small errors/typos.


Aucun commentaire:

Enregistrer un commentaire