[英]Error: Unexpected value 'undefined' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation
I am writing unit test case using jasmine for Angular using Nebular Module.我正在使用 jasmine 使用 Nebular Module 为 Angular 编写单元测试用例。 I am very new to writing test cases.我对编写测试用例很陌生。 From the below code when i use the fixture = TestBed.overrideComponent(..)
, I am getting the error:当我使用fixture = TestBed.overrideComponent(..)
从下面的代码中,我得到了错误:
Error: Unexpected value 'undefined' imported by the module 'DynamicTestModule'.错误:模块“DynamicTestModule”导入的意外值“未定义”。 Please add an @NgModule annotation.请添加 @NgModule 注释。
My code:我的代码:
import { ComponentRef } from "@angular/core";
import { async, ComponentFixture, TestBed } from "@angular/core/testing";
import { FormControl, ReactiveFormsModule } from "@angular/forms";
import { GeneratorComponent } from "./generator.component";
import { NbToastrModule, NbOverlayModule, NbLayoutScrollService } from '@nebular/theme';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { InjectionToken } from '@angular/core';
import { Injectable } from '@angular/core';
fdescribe('GeneratorComponent', () => {
let component: GeneratorComponent;
let fixture: ComponentFixture<GeneratorComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [GeneratorComponent],
imports: [
ReactiveFormsModule,
HttpClientTestingModule,
NbToastrModule.forRoot(),
NbOverlayModule.forRoot(),
{provide: InjectionToken,useValue: '',multi: true}
],
providers: [NbLayoutScrollService]
}).compileComponents();
fixture = TestBed.overrideComponent(GeneratorComponent, { //--Error line
set: {
selector: 'ngx-tabs',
templateUrl: './fm-generator.component.html'
}
})
.createComponent(GeneratorComponent);
//fixture = TestBed.createComponent(GeneratorComponent);
//component = fixture.componentInstance;
});
it('Test Which Application Template', () => {
console.log("Running test script for Factory Model");
const val = 10 * 2;
expect(val).toBe(20);
});
})
Where am i going wrong?我哪里错了? When i comment the fixture = TestBed.overrideComponent(GeneratorComponent, {} )
line, My test case is getting passed.当我评论fixture = TestBed.overrideComponent(GeneratorComponent, {} )
行时,我的测试用例正在通过。 Any problem with the usage of fixture in my case?在我的情况下使用夹具有什么问题吗?
You should get fixture
from TestBed.createComponent
method.您应该从TestBed.createComponent
方法中获取fixture
。 So it should be:所以应该是:
TestBed.configureTestingModule({--your code--})
.overrideComponent(--your code--)
.compileComponents();
fixture = TestBed.createComponent(GeneratorComponent);
Also I noticed strange line in imports:我还注意到导入中的奇怪行:
{provide: InjectionToken,useValue: '',multi: true}
it should be placed in providers, and instead of InjectionToken
some token that you have created and use in component它应该放在提供者中,而不是InjectionToken
一些您在组件中创建和使用的令牌
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.