gpt4 book ai didi

angular - 为什么我收到此错误 : NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService?

转载 作者:行者123 更新时间:2023-12-02 02:31:29 24 4
gpt4 key购买 nike

我正在上一门关于 Angular Testing 的类(class),我试图为一个带有服务的组件设置一个测试,这个服务有 httpClias 作为依赖项,但是当我尝试运行它时出现了这个错误

NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'MedicoService', 'HttpClient', 'HttpClient' ] })
NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
at NullInjector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10756:1)
at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10923:1)
at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10923:1)
at injectInjectorOnly (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:2290:1)
at ɵɵinject (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:2294:1)
at Object.MedicoService_Factory [as factory] (ng:///MedicoService/ɵfac.js:5:41)
at R3Injector.hydrate (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11091:1)
at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10912:1)
at NgModuleRef$1.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:24988:1)
at TestBedRender3.inject (node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1943:1)
Error: Expected undefined to be truthy.
at <Jasmine>
at UserContext.<anonymous> (src/app/intemedio2/medico/service/medico.service.spec.ts:14:21)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:364:1)
at ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/zone-testing.js:292:1)
Chrome 86.0.4240.198 (Windows 10): Executed 1 of 28 (1 FAILED) (0 secs / 0.119 secs)
Chrome 86.0.4240.198 (Windows 10) MedicoService should be created FAILED
NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
error properties: Object({ ngTempTokenPath: null, ngTokenPath: [ 'MedicoService', 'HttpClient', 'HttpClient' ] })
NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
at NullInjector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10756:1)
at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10923:1)
at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10923:1)
at injectInjectorOnly (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:2290:1)
at ɵɵinject (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:2294:1)
at Object.MedicoService_Factory [as factory] (ng:///MedicoService/ɵfac.js:5:41)
at R3Injector.hydrate (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:11091:1)
at R3Injector.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:10912:1)
at NgModuleRef$1.get (node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:24988:1)
at TestBedRender3.inject (node_modules/@angular/core/__ivy_ngcc__/fesm2015/testing.js:1943:1)
Error: Expected undefined to be truthy.
at <Jasmine>
at UserContext.<anonymous> (src/app/intemedio2/medico/service/medico.service.spec.ts:14:21)
at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:364:1)
Chrome 86.0.4240.198 (Windows 10): Executed 26 of 28 (1 FAILED) (skipped 2) (0.382 secs / 0.316 secs)

我只有这个服务 (medicosService)

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class MedicoService {

constructor(public http: HttpClient) { }



getMedicos() {

return this.http.get('....');
}
}

和这个组件

import { MedicoService } from './service/medico.service';
import { Component, OnInit } from '@angular/core';

@Component({
selector: 'app-medico',
templateUrl: './medico.component.html',
styleUrls: ['./medico.component.css']
})
export class MedicoComponent implements OnInit {

constructor(public medicoService: MedicoService) { }

medicos: any [] = []
ngOnInit(): void {
}

saludarMEdico(nombre: string): string {
return `hello ${nombre}`;
}


optenerMedicos() {
this.medicoService.getMedicos().subscribe((medicos: any []) => {
this.medicos = medicos;
})
}
}

这就是测试

import { MedicoService } from './service/medico.service';
import { ComponentFixture, TestBed } from '@angular/core/testing';
/** El testbed es una clase con muchos metodos para hacewr pruebas */
import { MedicoComponent } from './medico.component';
import { HttpClientModule } from '@angular/common/http';


describe('MedicoComponent', () => {
let component: MedicoComponent;
let fixture: ComponentFixture<MedicoComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MedicoComponent],
providers: [MedicoService],
imports:[HttpClientModule]
})
.compileComponents();
});

beforeEach(() => {
fixture = TestBed.createComponent(MedicoComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

//comprueba que el componente se cree
it('should create', () => {
expect(component).toBeTruthy();
});
// verificacion alternativa si el componente se creo, tenemos acceso a sus metodos
it('should return a greeting based on the sent name', () => {
const name = 'Gerardo';
const greeting = component.saludarMEdico(name);
expect(greeting).toContain(name);
});

});

我尝试在导入中使用 HttpClientTestingModule 而不是 HttpClientModule,但我遇到了同样的错误,所以我不知道发生了什么。

最佳答案

错误来自您的服务测试 medico.service.spec.ts 而不是您的组件测试。

例如在您的服务测试中试试这个:

describe('YourService', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
}));

it('should be created', () => {
const service: YourService = TestBed.get(YourService);
expect(service).toBeTruthy();
});
});

关于angular - 为什么我收到此错误 : NullInjectorError: R3InjectorError(DynamicTestModule)[MedicoService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64974552/

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