【问题标题】:Unexpected value 'ModalModule' imported by the module 'DynamicTestModule'模块“DynamicTestModule”导入的意外值“ModalModule”
【发布时间】:2021-06-19 15:15:03
【问题描述】:

我正在尝试测试使用 ngx-bootstrap 的模态指令的 MyComponent(如 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>

这是我已经尝试过的:

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

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

将ModalDirective添加到declarations列表中:

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

我做错了什么?

【问题讨论】:

  • 我认为这通常是缓存问题,尝试重新启动服务器,如果失败,请尝试重新启动 ide 和服务器
  • 感谢您的建议,但这是作为测试运行的,因此无需重新启动。
  • 对不起,我的意思是重新运行测试`
  • 试过了,也没用。还尝试清除所有缓存并核对node_modules,以防万一无济于事。
  • 问题可能出在您的实际代码中,而不是在测试中?确认在您的代码中您已导入 ModalModule.forRoot() 而不是 ModalModule

标签: angular jestjs ngx-bootstrap


【解决方案1】:

您还需要将 BsModalService 添加到您的 providers 数组中。

【讨论】:

  • 不幸的是这也不起作用,这个错误仍然出现:Unhandled Promise rejection: Unexpected value 'ModalModule' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.
猜你喜欢
  • 2017-06-03
  • 1970-01-01
  • 2017-05-07
  • 2020-01-06
  • 2018-05-24
  • 2020-11-23
  • 1970-01-01
  • 2017-12-11
  • 2022-10-13
相关资源
最近更新 更多