Error: Unexpected value 'CookieService' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation
Error: Unexpected value 'CookieService' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation
我正在尝试使用 jasmine 测试服务,但出现此错误:
错误:模块 'DynamicTestModule' 导入了意外值 'CookieService'。请添加@NgModule 注解。
和
错误:预期未定义为真。
我的规范文件:
import { RouterTestingModule } from '@angular/router/testing';
import { TestBed } from '@angular/core/testing';
import { Api } from './api.service';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, Inject, Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie';
fdescribe('ApiService', () => {
let service: Api;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
Api,
],
imports: [
HttpClientTestingModule,
RouterTestingModule,
CookieService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
});
});
beforeEach(() => {
service = TestBed.inject(Api);
httpMock = TestBed.inject(HttpTestingController);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
此错误消息显示为 CookieServie
不是 NgModule
。
Error: Unexpected value 'CookieService' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
Add the cookie service to your app.module.ts as a provider:
解决方案
您必须从 imports
部分删除 CookieService
并将其添加到 providers
部分。
.spec.ts
TestBed.configureTestingModule({
providers: [
Api,
CookieService
],
imports: [
HttpClientTestingModule,
RouterTestingModule
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
});
我正在尝试使用 jasmine 测试服务,但出现此错误:
错误:模块 'DynamicTestModule' 导入了意外值 'CookieService'。请添加@NgModule 注解。
和
错误:预期未定义为真。
我的规范文件:
import { RouterTestingModule } from '@angular/router/testing';
import { TestBed } from '@angular/core/testing';
import { Api } from './api.service';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, Inject, Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie';
fdescribe('ApiService', () => {
let service: Api;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
Api,
],
imports: [
HttpClientTestingModule,
RouterTestingModule,
CookieService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
});
});
beforeEach(() => {
service = TestBed.inject(Api);
httpMock = TestBed.inject(HttpTestingController);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
此错误消息显示为 CookieServie
不是 NgModule
。
Error: Unexpected value 'CookieService' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
Add the cookie service to your app.module.ts as a provider:
解决方案
您必须从 imports
部分删除 CookieService
并将其添加到 providers
部分。
.spec.ts
TestBed.configureTestingModule({
providers: [
Api,
CookieService
],
imports: [
HttpClientTestingModule,
RouterTestingModule
],
schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA],
});