在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);
});
});
});