简体   繁体   English

为什么我收到 NullInjectorError: R3InjectorError(DynamicTestModule)[MdbModalRef -> MdbModalRef]:?

[英]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.

相关问题 为什么我收到这个错误:NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService? - Why I'm getting this error : NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService? NullInjectorError:R3InjectorError(DynamicTestModule)[FormBuilder -&gt; FormBuilder]:NullInjectorError:没有FormBuilder的提供者 - NullInjectorError: R3InjectorError(DynamicTestModule)[FormBuilder -> FormBuilder]: NullInjectorError: No provider for FormBuilder R3InjectorError(DynamicTestModule)[Store -&gt; Store]: NullInjectorError: No provider for Store - R3InjectorError(DynamicTestModule)[Store -> Store]: NullInjectorError: No provider for Store NullInjectorError: R3InjectorError(DynamicTestModule)[ApiService -&gt; HttpClient -&gt; HttpClient]: NullInjectorError: 没有 HttpClient 的提供者 - NullInjectorError: R3InjectorError(DynamicTestModule)[ApiService -> HttpClient -> HttpClient]: NullInjectorError: No provider for HttpClient NullInjectorError: R3InjectorError(DynamicTestModule)[Router -&gt; Function -&gt; Function]: NullInjectorError: No provider for Function - NullInjectorError: R3InjectorError(DynamicTestModule)[Router -> Function -> Function]: NullInjectorError: No provider for Function Angular 8 使用 Karma 和 Jasmine 进行测试 NullInjectorError: R3InjectorError(DynamicTestModule)[api -&gt; api]: NullInjectorError: No provider for Z8A5DA52ED126447D359E7 - Angular 8 Testing with Karma and Jasmine NullInjectorError: R3InjectorError(DynamicTestModule)[api -> api]: NullInjectorError: No provider for api 错误 NullInjectorError:R3InjectorError(AppModule) - ERROR NullInjectorError: R3InjectorError(AppModule) 错误:未捕获(承诺):NullInjectorError:R3InjectorError(AppModule)[baseURL] - Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[baseURL] 错误:未捕获(承诺中):NullInjectorError:R3InjectorError(GestionGeneralModule) - Error: Uncaught (in promise): NullInjectorError: R3InjectorError(GestionGeneralModule) NullInjectorError: R3InjectorError(AppModule) 添加提供程序后 - NullInjectorError: R3InjectorError(AppModule) After adding providers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM