作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在上一门关于 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/
我是一名优秀的程序员,十分优秀!