mercredi 26 août 2015

How to test a controller mocking a service with jasmine

I am trying to test a angular controller mocking the calls made to a factory using jasmine. My controller is like this:

(function () {
  'use strict';

  function initScoresController(scoresFactory){
    var vm = this;
    vm.projects = {};
    vm.month = {};
    vm.month.users = {};
    vm.tally = {};
    vm.tally.projects = {};

    scoresFactory.projectsCurrentMonthScores().success(
      function(data){
        vm.projects = data;
      }
    );

    scoresFactory.usersCurrentMonthScores().success(
      function(data){
        vm.month.users = data;
      }
    );

    scoresFactory.projectCurrentMonthTally().success(
      function(data){
        vm.tally.projects = data;
      }
    );
  }

  angular.module('project.dashboard')
    .controller('ScoresController',  ['scoresFactory', initScoresController])
})();

My factory like this:

(function () {
  'use strict';

  function setupScoresFactory($http, PROJECTS_CURRENT_MONTH_SCORES, USERS_CURRENT_MONTH_SCORES, PROJECT_CURRENT_MONTH_TALLY){

    var scoresFactory = {};

    scoresFactory.projectsCurrentMonthScores = function(){
      return $http.get(PROJECTS_CURRENT_MONTH_SCORES);
    };

    scoresFactory.usersCurrentMonthScores = function(){
      return $http.get(USERS_CURRENT_MONTH_SCORES);
    };

    scoresFactory.projectCurrentMonthTally = function(){
      return $http.get(PROJECT_CURRENT_MONTH_TALLY);
    };

    return scoresFactory;

  };

  angular
    .module('project.dashboard', ["config"])
    .factory('scoresFactory', ['$http', 'PROJECTS_CURRENT_MONTH_SCORES', 'USERS_CURRENT_MONTH_SCORES', 'PROJECT_CURRENT_MONTH_TALLY', setupScoresFactory]);
})();

And my test:

describe('dashboard scores controller', function(){
 describe('projectCurrentMonthTally function', function(){
   var controller, scoresCtrl, scoresFactoryMock;

   beforeEach(module('config'));
   beforeEach(module('project.dashboard'));

   beforeEach(
     inject(function(_$controller_, _scoresFactory_){
       controller = _$controller_;
       scoresFactoryMock = _scoresFactory_;

       jasmine.spyOn(scoresFactoryMock, 'projectsCurrentMonthScores').andReturn(function(){console.log('fake project current month')});

       scoresCtrl = controller('ScoresController', {scoresFactory: scoresFactoryMock});
     }));

    it('it should return a collection of votes for every project', function(){
      expect(scoresFactoryMock.projectsCurrentMonthScores).toHaveBeenCalled();
    });
 });
});

My test throws this error:

TypeError: undefined is not a function
    at Object.<anonymous> (http://localhost:8081/base/test/spec/dashboard/dashboard.scores.controller.js:13:16)
    at Object.invoke (http://localhost:8081/base/bower_components/angular/angular.js:4473:17)
    at Object.workFn (http://localhost:8081/base/bower_components/angular-mocks/angular-mocks.js:2401:20)

I believe that the problem is on my success() calls. How can I test this controller, mocking the calls made to my factory?

Aucun commentaire:

Enregistrer un commentaire