gpt4 book ai didi

angular - 错误 NullInjectorError : R3InjectorError(DynamicTestModule)[KDSDialogService -> MatDialog -> MatDialog]: NullInjectorError: No provider for MatDialog

转载 作者:行者123 更新时间:2023-12-02 18:36:33 25 4
gpt4 key购买 nike

我正在对我的应用程序进行单元测试。我是测试新手,因此需要您的帮助。

在我的应用程序中,我创建了一个使用 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();
});
});

Print of the error

最佳答案

你可以这样模拟它:

import { MatDialog } from '@angular/material/dialog';
//...
let matDialogService: jasmine.SpyObj<MatDialog>;
matDialogService = jasmine.createSpyObj<MatDialog>('MatDialog', ['open']);

TestBed.configureTestingModule({
providers: [
{
provide: MatDialog,
useValue: matDialogService,
},

关于angular - 错误 NullInjectorError : R3InjectorError(DynamicTestModule)[KDSDialogService -> MatDialog -> MatDialog]: NullInjectorError: No provider for MatDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68682619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com