Jasmin + karma:“错误:模块'DynamicTestModule'导入了意外的值'HttpClient'.请添加@NgModule批注. [英] Jasmin + karma: "Error: Unexpected value 'HttpClient' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation."
问题描述
我将茉莉花用作测试框架,将业力用作测试运行程序.我正在尝试创建HttpClient对象,以便可以创建作为对此对象的依赖的服务:
I'm using jasmine as a test framework and karma as a test runner. I'm trying to create an HttpClient object so I could create a service that as a depedency to this object:
TestBed.configureTestingModule({
declarations: [HttpClient],
imports: [HttpClient],
providers: [HttpClient]
});
TestBed.get(HttpClient);
但是我遇到了以下错误:
But I'm getting the following error:
错误:模块'DynamicTestModule'导入了意外的值'HttpClient'.请添加@NgModule批注.
Error: Unexpected value 'HttpClient' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
有人知道如何解决这个问题吗?
Any one have an idea how to solve this?
遵循所有代码:
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();
});
});
推荐答案
当您尝试在declarations
数组中包含除组件,指令或管道以外的内容时,会引发编译错误.
The compilation error you get is thrown when you try to include something other than a component, directive, or pipe in the declarations
array.
我已经重构了您的测试规范,从声明模块中删除了HttpClient
,导入了HttpClientTestingModule
,因为对于
I've refactored your test spec to remove the HttpClient
from the declarations module, import the HttpClientTestingModule
since it has some significant advantages over the HttpClientModule
for testing, and used a slightly different pattern to create an instance of your I18nService
to pass to your model class.
import { HttpClientTestingModule } from '@angular/common/http/testing';
describe('TestSpec', () => {
let intlService = I18nService;
beforeAll(() => {
TestBed.configureTestingModule({
declarations: [],
imports: [HttpClientTestingModule],
providers: [I18nService]
});
i18nService = TestBed.Get(I18nService);
});
这篇关于Jasmin + karma:“错误:模块'DynamicTestModule'导入了意外的值'HttpClient'.请添加@NgModule批注.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!