【问题标题】:NullInjectorError: No provider for HttpClient , throws error even in http client is injectedNullInjectorError:没有 HttpClient 的提供者,即使在 http 客户端被注入时也会抛出错误
【发布时间】:2021-08-19 12:41:44
【问题描述】:
 import { HttpClientModule } from '@angular/common/http';
        import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
        import { NO_ERRORS_SCHEMA } from '@angular/core';
        import { async, ComponentFixture, TestBed } from '@angular/core/testing';
        import { RouterModule, Routes } from '@angular/router';
        import { DrawerService} from '../services/create-work-order-confirmation-drawer.service';
        import { HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
        import { ConfirmationDrawerComponent } from './create-work-order-confirmation-drawer.component';
        import { FormsModule } from '@angular/forms';
        
        describe('ConfirmationDrawerComponent ', () => {
          let component: ConfirmationDrawerComponent ;
          let fixture: ComponentFixture<ConfirmationDrawerComponent >;
          let CreateServices = DrawerService;
        
          beforeEach(async (() => {
             TestBed.configureTestingModule({
              declarations: [ ConfirmationDrawerComponent ],
              imports: [FormsModule, HttpClientTestingModule,RouterModule.forRoot([])],
              providers: [{
                provide: DrawerService
              }],
              schemas: [NO_ERRORS_SCHEMA, CUSTOM_ELEMENTS_SCHEMA]
            })
            .compileComponents();
          }));
        
          beforeEach(() => {
            fixture = TestBed.createComponent(ConfirmationDrawerComponent );
            component = fixture.componentInstance;
            CreateServices = TestBed.get(DrawerService);
            fixture.detectChanges();
          });
        
          it('should create', () => {
            expect(component).toBeTruthy();
          });
        });

不知道是什么问题! 我错过了什么? 我已将所有模块添加到导入中,我错过了什么吗? 导入:[FormsModule, HttpClientTestingModule,RouterModule.forRoot([])],

【问题讨论】:

  • 你能发布错误的完整堆栈跟踪吗
  • 我已添加图片

标签: angular karma-jasmine karma-runner karma-coverage


【解决方案1】:

错误来自CreateWorkOrderConfirmationDrawerService 的规范文件,转到该文件并检查是否导入了HttpClientTestingModule

【讨论】:

    猜你喜欢
    • 2019-06-25
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多