我有一个要测试的简单组件。我从另一个加载这个组件作为模式:
const modal = this.modalService.create({
nzContent: MyModalComponent
});
组件
@Component({
selector: 'my-modal-component',
template: `
<div *nzModalFooter>
<button nz-button nzType="default" (click)="destroyModal()">
Cancel
</button>
</div>
`
})
export class MyModalComponent{
constructor(private modal: NzModalRef) {}
destroyModal(): void {
this.modal.destroy({});
}
}
为组件创建测试台时MyModalComponent
,出现错误:
NullInjectorError: StaticInjectorError(DynamicTestModule)[NzModalFooterDirective -> NzModalRef]:
StaticInjectorError(Platform: core)[NzModalFooterDirective -> NzModalRef]:
NullInjectorError: No provider for NzModalRef!
阅读我尝试使用的错误NzModalFooterDirective
。
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [OtherModules],
declarations: [MyModalComponent],
providers: [
NzModalFooterDirective
]
}).compileComponents();
}));
和任何其他进口:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [OtherModules],
declarations: [MyModalComponent],
providers: [
{ provide: NZ_MODAL_CONFIG, useValue: {} },
NzModalService,
NzModalFooterDirective
]
}).compileComponents();
}));
没有成功...
我也尝试直接使用 NzModalRef
describe('MyModalComponent', () => {
let component: MyModalComponent;
let fixture: ComponentFixture<MyModalComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [OtherModules],
declarations: [MyModalComponent],
providers: [NzModalRef]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MyModalComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
但后来我得到了错误:
this.nzModalRef.getInstance is not a function
我只是想准备试验台。我不确定我是导入NzModalRef
错误还是必须用自定义模拟替换它。