我已经定义了自己的表单类,该类通过以下构造函数扩展了FormGroup:
public constructor(/* params */) {
function myValidator(): ValidatorFn {
//return validator function
}
super({ /* controls */}, [myValidator()]);
}
当我运行我的应用程序时,它可以工作,但是当我对其运行单元测试时,它在超级构造函数上中断,并且出现以下错误消息:
TypeError:如果没有'new',则无法调用类构造函数FormGroup
它是在组件的ngOnInit函数中构造的,就像这样:
ngOnInit() {
this.myForm = new MyForm(/* args */);
//call service
}
我的规格文件:
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
const myArray = ['object'];
let myServiceMock: any;
let getCallSpy: any;
describe('ErdwwLoggingComponent', () => {
beforeEach(async(() => {
myServiceMock = jasmine.createSpyObj('MyService', ['getCall']);
getCallSpy = myService.getCall.and.returnValue( of(myArray) );
TestBed.configureTestingModule({
declarations: [
MyComponent
],
providers: [
{ provide: MyService, useValue: myServiceMock }
]
});
TestBed.overrideComponent(MyComponent, {
remove: {
templateUrl: './my.component.html'
},
add: {
template: '<div>test</div>'
}
});
TestBed.compileComponents().then(() => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
});
}));
//succeeds
it('should have spies defined', () => {
expect(myServiceMock.getCall).toBeDefined('myServiceMock.getCall not defined');
expect(getCallSpy).toBeDefined('getCallSpy not defined');
});
describe('initialisation', () => {
//succeeds
it('should be defined', () => {
expect(component).toBeDefined();
});
//fails
it('should call getCall', () => {
// I also tried explicitly calling component.ngOnInit();
fixture.detectChanges(); // onInit()
expect(myServiceMock.getCall).toHaveBeenCalled();
});
});
});
答案 0 :(得分:0)
问题出在我的tsconfig.spec.json文件中。
在compilerOptions
中,我有"target": "es5"
,当我删除该错误后,我不再收到错误消息。