首页 > 解决方案 > 模块“DynamicTestModule”导入的意外值“ModalModule”

问题描述

我正在尝试测试MyComponent哪个使用 ngx-bootstrap 的模态指令(如https://valor-software.com/ngx-bootstrap/#/modals#directive-section所示)。该组件本身可以完美运行,但是在尝试对其进行测试时,出现此错误:

Unhandled Promise rejection: Unexpected value 'ModalModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.

这就是我的 TestBed 的样子:

TestBed.configureTestingModule({
  imports: [
    HttpClientTestingModule,
    RouterTestingModule.withRoutes([]),
    ModalModule.forRoot()
  ],
  declarations: [MyComponent],
  providers: [],
  schemas: [ NO_ERRORS_SCHEMA ]
}).compileComponents().then(() => {
  fixture = TestBed.createComponent(MyComponent);
  component = fixture.componentInstance;
});

这就是模态的设置方式MyComponent

export class MyComponent implements {
  @ViewChild('confirmDeleteModal')
  confirmDeleteView: ModalDirective;

  constructor() {} // empty constructor just to illustrate the fact that there are no other dependencies

作为参考,这是我的模板的相关部分:

<div bsModal #confirmDeleteModal="bs-modal" class="modal fade" role="dialog"> ... </div>

这是我已经尝试过的:

ModalModule.forRoot()从列表中删除imports,然后给我这个错误:

Unhandled Promise rejection: Export of name 'bs-modal' not found!

添加ModalDirective到列表中declarations

Unhandled Promise rejection: R3InjectorError(DynamicTestModule)[ComponentLoaderFactory -> ComponentLoaderFactory]: 
  NullInjectorError: No provider for ComponentLoaderFactory!

我究竟做错了什么?

标签: angularjestjsngx-bootstrap

解决方案


您还需要添加BsModalService到您的提供者数组。


推荐阅读