tcoulson
tcoulson

Reputation: 624

testing a function on rootscope using karma, not a function

I have the following function in my controller:

.controller('ContainerController', ['$scope', '$rootScope', '$state','$window', '$location','employeeFactory', 'employeeTestFactory', function ($scope, $rootScope, $state, $window,$location, employeeFactory, employeeTestFactory) {
        $rootScope.calculateUsed = function(val){
            $rootScope.employee.timePending = $rootScope.employee.timePending = 0;
            var newTimeUsed = 0;
            angular.forEach(val, function(key, value){
                var td = key.timeDuration;
                if(key.timeState === "pending"){
                    $rootScope.employee.timePending += Number(td);
                }else{
                    newTimeUsed += Number(td);
                }
            });
            $rootScope.employee.totalTimeUsed = newTimeUsed;
        }
 }])

Inside my karma I am trying to declare and use the function:

describe('Controllers', function() {
  var InfoController, ContainerController, state;
  var $controller, $location, $window;
  var scopeIC, scopeCC;

  beforeEach(module('ui.router'));
  beforeEach(angular.mock.module('ptoApp.controller'));
  beforeEach(angular.mock.module('ptoApp.employeeTestFactory'));
  beforeEach(module('ngResource'));


  beforeEach(inject(function($controller, _$rootScope_, _$location_, _$window_, _employeeTestFactory_, _$httpBackend_, _$state_) {
    scopeIC = _$rootScope_.$new();
    scopeCC = _$rootScope_.$new();
    $rootScope = _$rootScope_;
    $httpBackend = _$httpBackend_;

    ContainerController = $controller('InfoController', {
      $scope: scopeCC,
      $location: _$location_,
      $window: _$window_,
      $rootScope: $rootScope,
      employeeTestFactory:_employeeTestFactory_,
      $state:_$state_
    });
  }));

  it('ContainerController should be defined', function() {
    expect(ContainerController).toBeDefined();

  });

  it('ContainerController should test calculateUsed on rootscope', function() {
    $rootScope.employee = {
      "employeeid": "[email protected]",
      "firstName": "Todd",
      "lastName": "Coulson",
      "employeeType": "full-time",
      "totalTimeUsed": 0,
      "totalTimeAccrued": 40
    }
    var singleReq = [{"requestedBy": "Todd",
        "approvedBy": "Tom",
        "status": "approved",
        "startDateTime": "3/25/2017",
        "endDateTime": "3/29/2017",
        "timeDuration": "32",
        "message": "I need a vaca",
        "approverMessage": "Take one!",
        "locked": "false",
        "timeState":"approved",
        "timeType":"multiple days",
        "timeOffGroup":"paid time off"}]

    $rootScope.calculateUsed(singleReq);
    expect($rootScope.employee.totalTimeUsed).toBe(32);
  });
});

Can anyone tell me why calculateUsed is not a function here?I am passing in the rootScope, I can verify it is defined, but it cannot read a function I have put on the $rootScope? Thanks in advance.

Upvotes: 0

Views: 407

Answers (2)

RafaelTSCS
RafaelTSCS

Reputation: 1314

You are calling ContainerController as 'InfoController'.

ContainerController = $controller('InfoController', { ...})

You should call it properly as you named it on controller declaration:

ContainerController = $controller('ContainerController', { ...}) 

Upvotes: 1

tcoulson
tcoulson

Reputation: 624

I had a typo in terms of calling the correct Controller. Once I fixed and imported the correct dependencies, it worked.

Upvotes: 0

Related Questions