使用业力而不是函数在根镜上测试函数

时间:2017-05-04 15:08:02

标签: angularjs karma-runner karma-jasmine

我的控制器中有以下功能:

.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;
        }
 }])

在我的业力中,我试图宣布并使用该函数:

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": "tcoulson@gmail.com",
      "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);
  });
});

有人可以告诉我为什么calculateUsed不是这里的函数吗?我在rootScope中传递,我可以验证它是否已定义,但它无法读取我在$ rootScope上放置的函数?提前致谢。

1 个答案:

答案 0 :(得分:0)

我在调用正确的控制器时遇到了错字。一旦我修复并导入了正确的依赖项,它就可以了。