首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ERROR NullInjectorError: R3InjectorError(DynamicTestModule)[KDSDialogService -> MatDialog -> MatDialog]:NullInjectorError:没有MatDialog的提供程序

问ERROR NullInjectorError: R3InjectorError(DynamicTestModule)[KDSDialogService -> MatDialog -> MatDialog]:NullInjectorError:没有MatDialog的提供程序
EN

Stack Overflow用户
提问于 2021-08-06 13:43:20
回答 1查看 642关注 0票数 2

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

在我的应用程序中,我创建了一个使用MatDialog ( KDSDialogService)的服务。我试着把许多导入选项,我的服务或matdialog作为提供者,我不知道该怎么做

代码语言:javascript
复制
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中,我在这里导入并进行了声明,但我仍然收到这个错误。

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

发布于 2021-08-06 13:47:03

你可以这样模拟它:

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

    TestBed.configureTestingModule({
      providers: [
        {
          provide: MatDialog,
          useValue: matDialogService,
        },
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68682619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档