【问题标题】:Failed: Unexpected value 'TreeviewConfig' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation in unit test失败:模块“DynamicTestModule”导入的意外值“TreeviewConfig”。请在单元测试中添加@NgModule 注解
【发布时间】:2021-02-08 15:35:30
【问题描述】:

我正在尝试测试一个包含 ngx-treeview 的组件。我写了以下测试,但它失败了,标题中包含错误消息(显然除了 in unit test 部分):

import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { TreeviewConfig, TreeviewItem, TreeviewModule } from 'ngx-treeview';

import { TabTreeviewComponent } from './tab-treeview.component';

describe('TabTreeviewComponent', () => {
  let component: TabTreeviewComponent;
  let fixture: ComponentFixture<TabTreeviewComponent>;

  beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [ TabTreeviewComponent ],
      imports: [ TreeviewConfig, TreeviewItem, TreeviewModule.forRoot() ],
      providers: [  ]
    })
    .compileComponents();
  }));



  beforeEach(() => {
    fixture = TestBed.createComponent(TabTreeviewComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    const config = TreeviewConfig.create({​​
      hasAllCheckBox: false,
      hasFilter: true,
      hasCollapseExpand: true,
      maxHeight: 435
    }​​);
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

我已经尝试将 TreeviewConfig 初始化为声明,但显然没有任何用处。我还尝试将它作为提供者提供 - 这也没有任何帮助......我不确定此时我还能尝试什么,TestBed 必须有办法以某种方式包含 TreeView 的配置,或者我是否在做一个合乎逻辑的这里有谬误?

【问题讨论】:

    标签: javascript angular typescript treeview ngx-treeview


    【解决方案1】:

    由于您在 inports 数组中包含非模块,因此引发错误

    基本上只需要导入TreeviewModule

     imports: [ TreeviewModule.forRoot() ],
    

    【讨论】:

    • .forRoot() 有必要吗?它有什么作用?
    • forRoot() 很重要。如果您不包含它,某些功能将无法使用
    猜你喜欢
    • 2018-01-12
    • 2019-07-12
    • 2018-05-24
    • 2022-10-13
    • 2020-05-07
    • 2021-10-07
    • 2020-12-27
    • 2022-01-22
    • 2023-03-12
    相关资源
    最近更新 更多