TypeError:矩不是函数-Angularjs茉莉花测试用例

时间:2019-10-31 07:35:44

标签: unit-testing jasmine karma-jasmine

在AngularJs中为控制器编写测试用例时遇到问题。

我发现的原因:当我在构造函数中初始化时刻时,在运行测试用例时出现此错误。

我正在共享控制器代码和测试文件。

  class contractCancellationCtrl {
    constructor(
            moment) {
  'ngInject';
  this.moment = moment;
  this.minSalesDate = moment().subtract(1, 'months').date(1);

  }

define(['contract'], () => {
  'use strict';

  let momentMock,
      ctrl,
      $q,
      $scope;
  
  const dateMock = "Feb 16, 2018 10:22 AM";

  fdescribe('contractCancellation', () => {
    beforeEach(() => {
      module('nse.contract');

      momentMock = jasmine.createSpyObj('moment', ['subtract']);
      momentMock.subtract.and.returnValue(dateMock);

      inject((_$rootScope_, _$controller_, _$q_) => {
        $scope = _$rootScope_.$new();
        $q = _$q_;
        ctrl = _$controller_('contractCancellationCtrl as contractCancellationCtrl', {
          moment: momentMock,
        });
      });

      contractCancellationDeferred = $q.defer();
    });

    it('should expect the controller to be initialized', () => {
      expect(ctrl.moment).toEqual(momentMock);

    });
  });
});

0 个答案:

没有答案