typescript Jasmin + karma:“错误:模块‘DynamicTestModule’导入的意外值‘HttpClient’。请添加@NgModule 注释。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52116993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmin + karma: "Error: Unexpected value 'HttpClient' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation."
提问by Ricardo Rocha
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:
我使用 jasmine 作为测试框架,使用 karma 作为测试运行器。我正在尝试创建一个 HttpClient 对象,以便我可以创建一个服务,作为对这个对象的依赖:
TestBed.configureTestingModule({
declarations: [HttpClient],
imports: [HttpClient],
providers: [HttpClient]
});
TestBed.get(HttpClient);
But I'm getting the following error:
但我收到以下错误:
Error: Unexpected value 'HttpClient' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.
错误:模块“DynamicTestModule”导入的意外值“HttpClient”。请添加@NgModule 注释。
Any one have an idea how to solve this?
任何人都知道如何解决这个问题?
Follows all the code:
遵循所有代码:
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();
});
});
回答by ericksoen
The compilation error you get is thrown when you try to include something other than a component, directive, or pipe in the declarations
array.
当您尝试在declarations
数组中包含除组件、指令或管道以外的内容时,会引发编译错误。
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.
我重构了你的测试规范,HttpClient
从声明模块中删除了,导入了,HttpClientTestingModule
因为它比HttpClientModule
for testing有一些显着的优势,并使用稍微不同的模式来创建你的实例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);
});
回答by Shanil Arjuna
You have to import HttpClientModule in your module file
您必须在模块文件中导入 HttpClientModule
import {HttpClientModule} from '@angular/common/http';