我将茉莉花用作测试框架,将业力用作测试运行程序。我正在尝试创建HttpClient对象,因此可以创建一个服务作为对此对象的依赖:
TestBed.configureTestingModule({
declarations: [HttpClient],
imports: [HttpClient],
providers: [HttpClient]
});
TestBed.get(HttpClient);
但是我遇到以下错误:
错误:模块'DynamicTestModule'导入了意外的值'HttpClient'。请添加@NgModule批注。
有人知道如何解决这个问题吗?
遵循所有代码:
import { I18nService } from "../../services/i18n.service";
import { TestBed, inject, async } from "@angular/core/testing";
import { EditionHistoryEventsModel } from "./dropdown.edition.history.events.model";
import { HttpClient } from "@angular/common/http";
import { TestUtil } from "../../utils/test.uti";
describe('DropDownEditionHistoryItemModel', () => {
let i18nService: I18nService;
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [HttpClient],
imports: [HttpClient],
providers: [HttpClient]
});
i18nService = TestUtil.geti18nService(TestBed.get(HttpClient));
});
it('asdasd', () => {
let model: EditionHistoryEventsModel = new EditionHistoryEventsModel(i18nService);
expect(true).toBeTruthy();
});
});
答案 0 :(得分:6)
当您尝试在declarations
数组中包括组件,指令或管道以外的内容时,会引发编译错误。
我已经重构了您的测试规范,以便从声明模块中删除HttpClient
,并导入HttpClientTestingModule
,因为它比testing的HttpClientModule
有很多优势,并使用了稍微不同的模式来创建I18nService
的实例以传递给模型类。
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('TestSpec', () => {
let intlService = I18nService;
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [],
imports: [HttpClientTestingModule],
providers: [I18nService]
});
i18nService = TestBed.Get(I18nService);
});
答案 1 :(得分:0)
您必须在模块文件中导入HttpClientModule
import {HttpClientModule} from '@angular/common/http';
答案 2 :(得分:-1)
在我的情况下,当我导入覆盖模块时错误解决
import {OverlayModule} from '@angular/cdk/overlay';
...
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [...],
imports: [OverlayModule],
providers: [...]
});