【发布时间】:2021-05-25 22:13:28
【问题描述】:
我正在尝试创建一个 Angular 单元测试,但马上就失败了。
我已经查看了this 和 this 这个 SO,这似乎正是我正在努力解决的问题,但在尝试了那里提到的修复后,我仍然不正确。
这是我的代码:
import { TestBed } from '@angular/core/testing';
import { ContactsService } from './contacts.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClient } from '@angular/common/http';
describe('ContactsService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [ HttpClientTestingModule, ContactsService, HttpClient],
})
.compileComponents();
});
});
当我运行一个 ng 测试时,我得到了这个错误:
NullInjectorError: R3InjectorError(DynamicTestModule)[ContactsService -> HttpClient -> HttpClient]: NullInjectorError: 没有 HttpClient 的提供者!
我的 .ts,为了完整性:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Entry } from '../classes/entry';
import { EntrySearch } from '../classes/entrySearch';
@Injectable({
providedIn: 'root'
})
export class ContactsService {
constructor(private http: HttpClient) { }
}
【问题讨论】:
-
这个关于重复目标的答案完美回答:stackoverflow.com/a/47261579/5468463
标签: angular unit-testing