sou*_*ato 2 testing jasmine karma-runner angular
我正在对我的应用程序进行单元测试。我是测试新手,所以我需要你的帮助。
\n在我的应用程序中,我创建了一个使用 MatDialog ( KDSDialogService) 的服务。\n我尝试将许多导入替代方案、我的服务或 matdialog 作为提供程序,但我不知道该怎么做
\nexport declare class KDSDialogService {\n dialog: MatDialog;\n private dialogRef;\n constructor(dialog: MatDialog);\n open(componentOrTemplateRef: ComponentType<any> | TemplateRef<any>, title?: string, data?: any, size?: DialogSize, showClose?: boolean): MatDialogRef<any, any>;\n static \xc9\xb5fac: \xc9\xb5ngcc0.\xc9\xb5\xc9\xb5FactoryDef<KDSDialogService, never>;\n}\nRun Code Online (Sandbox Code Playgroud)\n在我的 home.component.spec 中,我在此处导入并进行声明,但仍然收到此错误。
\ndescribe(\'HomeComponent\', () => {\n let component: HomeComponent;\n let fixture: ComponentFixture<HomeComponent>;\n\n beforeEach(async(() => {\n TestBed.configureTestingModule({\n declarations: [HomeComponent ],\n imports:[KDSDialogService, MatDialogModule],\n \n }).compileComponents();\n }));\n\n beforeEach(() => {\n fixture = TestBed.createComponent(HomeComponent);\n component = fixture.componentInstance;\n \n });\n\n it(\'should create\', () => {\n fixture.detectChanges();\n expect(component).toBeTruthy();\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n
你可以这样嘲笑它:
import { MatDialog } from '@angular/material/dialog';
//...
let matDialogService: jasmine.SpyObj<MatDialog>;
matDialogService = jasmine.createSpyObj<MatDialog>('MatDialog', ['open']);
TestBed.configureTestingModule({
providers: [
{
provide: MatDialog,
useValue: matDialogService,
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5534 次 |
| 最近记录: |