简体   繁体   English

错误:模块“DynamicTestModule”导入的意外值“未定义”。 请添加@NgModule 注释

[英]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.

相关问题 由模块“DynamicTestModule”导入的意外值“DecoratorFactory”。 请添加@NgModule 注释 - Unexpected value 'DecoratorFactory' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation Jasmin + karma:“错误:模块‘DynamicTestModule’导入的意外值‘HttpClient’。请添加@NgModule 注释。” - Jasmin + karma: "Error: Unexpected value 'HttpClient' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation." 错误:由模块“DynamicTestModule”导入的意外值“DomSanitizer”。 请添加@NgModule 注释 - Error: Unexpected value 'DomSanitizer' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation 模块“DynamicTestModule”导入的意外指令“InformationComponent”。 请添加@NgModule 注解 - Unexpected directive 'InformationComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation 模块 DynamicTestModule 导入的意外值 DecoratorFactory。 请添加@NgModule 注解 - Unexpected value DecoratorFactory imported by the module DynamicTestModule. Please add a @NgModule annotation 使用 Spectator 进行 Angular 单元测试 - 模块“DynamicTestModule”导入的意外值“FormBuilder”。 请添加@NgModule 注释 - Angular Unit Testing with Spectator - Unexpected value 'FormBuilder' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation 失败:模块“DynamicTestModule”导入的意外值“TreeviewConfig”。 请在单元测试中添加@NgModule 注解 - Failed: Unexpected value 'TreeviewConfig' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation in unit test 单元测试期间的 Auth0 Angular 错误:模块“DynamicTestModule”导入的值“AuthModule”异常。 请添加@NgModule 注释 - Auth0 Angular error during unit test: Unexpected value 'AuthModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation 失败:模块&#39;DynamicTestModule&#39;导入的意外指令&#39;ContactDetailsComponent&#39;。 请添加@NgModule注释 - Failed: Unexpected directive 'ContactDetailsComponent' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation 模块&#39;AppModule&#39;导入的意外值&#39;MDCTextField&#39;。 请添加@NgModule注释 - Unexpected value 'MDCTextField' imported by the module 'AppModule'. Please add a @NgModule annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM