【发布时间】:2021-06-14 00:11:34
【问题描述】:
我在 Angular 中使用材质对话框并编写测试来测试对话框的关闭 我收到错误预期是间谍,但未定义。在测试中出现错误 fit('应该调用函数关闭对话框'
describe('XYZDetailsComponent', () => {
let component: XYZDetailsComponent;
let fixture: ComponentFixture<XYZDetailsComponent>;
const data = {
ip: '1.1.1.1',
name: 'test'
}
const setupComponent = () => {
fixture = TestBed.createComponent(XYZDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, MatDialogModule ],
declarations: [XYZDetailsComponent],
providers: [
{ provide: MatDialogRef, useFactory: () => jasmine.createSpyObj('MatDialogRef', ['close', 'afterClosed']) },
{provide: MAT_DIALOG_DATA, useValue: data},
]
})
.compileComponents();
});
it('should create', () => {
setupComponent();
expect(component).toBeTruthy();
});
fit('should call the function to close the dialog', () => {
setupComponent();
component.onNoClick();
expect(component.dialogRef.close()).toHaveBeenCalled();
});
});
【问题讨论】: