[英]Why am i getting NullInjectorError: R3InjectorError(DynamicTestModule)[MdbModalRef -> MdbModalRef]:?
I have an angular app in which i have component called CreateComponent
which opens as a modal
.我有一个 angular 应用程序,其中我有一个名为
CreateComponent
的组件,它以modal
打开。 That component is working fine.该组件工作正常。 Now i am writing
unit tests
for that CreateComponent
现在我正在为该
CreateComponent
编写unit tests
So i provided all of my dependencies like this所以我像这样提供了我所有的依赖项
import { CommonModule } from '@angular/common'; import { HttpClientModule } from '@angular/common/http'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { GlobalConstants } from '@constants/global.constants'; import { MDBBootstrapModule } from 'angular-bootstrap-md'; import { MockDependenciesModule } from 'app/modules/mock-dependecies/mock-dependencies.module'; import { MdbFormsModule } from 'mdb-angular-ui-kit/forms'; import { MdbModalModule, MdbModalService } from 'mdb-angular-ui-kit/modal'; import { ToastrModule, ToastrService } from 'ngx-toastr'; import { CreateComponent } from './create.component'; describe('CreateComponent', () => { let component: CreateComponent; let fixture: ComponentFixture<CreateComponent>; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ CreateComponent ], imports: [ HttpClientModule, ToastrModule.forRoot(), MdbModalModule, CommonModule, BrowserModule, MDBBootstrapModule.forRoot(), FormsModule, ReactiveFormsModule, MdbFormsModule, MockDependenciesModule, ], providers: [ToastrService, MdbModalService] }).compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(CreateComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); });
But when i run the test so it is givine me an error like this但是当我运行测试时,它给了我这样的错误
NullInjectorError: R3InjectorError(DynamicTestModule)[MdbModalRef -> MdbModalRef]:
NullInjectorError: No provider for MdbModalRef!
So every time my test fails.所以每次我的测试失败。 What's wrong here?
这里有什么问题?
Note: I am using mdb bootstrap
注意:我正在使用
mdb bootstrap
I have to make some assumptions about your code, as you have not posted the component.ts code, but it seems that you have declared an MdbModalRef
as a constructor parameter.我必须对您的代码做出一些假设,因为您尚未发布 component.ts 代码,但您似乎已将
MdbModalRef
声明为构造函数参数。
MdbModalRef
s are not meant to be injected directly. MdbModalRef
并不意味着直接注入。 Instead, you should obtain one using an MdbModalService
, like so相反,您应该使用
MdbModalService
获得一个,就像这样
export class CreateComponent {
modalRef: MdbModalRef<any>;
constructor(private modalService: MdbModalService) {}
// ...
showModal(modal: TemplateRef<any>) {
this.modalRef = modalService.open(modal);
}
}
of course, your code will look a bit different based on the scenario当然,根据场景,您的代码看起来会有所不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.