我正在对我的应用程序进行单元测试。我是测试新手,所以我需要你的帮助。
在我的应用程序中,我创建了一个使用MatDialog ( KDSDialogService)的服务。我试着把许多导入选项,我的服务或matdialog作为提供者,我不知道该怎么做
export declare class KDSDialogService {
dialog: MatDialog;
private dialogRef;
constructor(dialog: MatDialog);
open(componentOrTemplateRef: ComponentType<any> | TemplateRef<any>, title?: string, data?: any, size?: DialogSize, showClose?: boolean): MatDialogRef<any, any>;
static ɵfac: ɵngcc0.ɵɵFactoryDef<KDSDialogService, never>;
}在我的home.component.spec中,我在这里导入并进行了声明,但我仍然收到这个错误。
describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [HomeComponent ],
imports:[KDSDialogService, MatDialogModule],
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
});
it('should create', () => {
fixture.detectChanges();
expect(component).toBeTruthy();
});
});发布于 2021-08-06 13:47:03
你可以这样模拟它:
import { MatDialog } from '@angular/material/dialog';
//...
let matDialogService: jasmine.SpyObj<MatDialog>;
matDialogService = jasmine.createSpyObj<MatDialog>('MatDialog', ['open']);
TestBed.configureTestingModule({
providers: [
{
provide: MatDialog,
useValue: matDialogService,
},https://stackoverflow.com/questions/68682619
复制相似问题