简体   繁体   English

角度测试依赖模块

[英]Angular test dependency module

I am successfully unit-testing a component-under-test. 我正在成功地对被测组件进行单元测试。 I had to add some more functionality to the component - tooltips and translations. 我必须向该组件添加更多功能-工具提示和翻译。 The translation service is my code and I was able to both test component-under-test with the translation service and also with mocked translation service. 翻译服务是我的代码,我既可以使用翻译服务也可以使用模拟翻译服务来测试被测组件。

The issue I am having is the tooltips. 我遇到的问题是工具提示。 Those come from ng-bootstrap and are usable via importing NgbModule.forRoot() in my app.module. 这些来自ng-bootstrap,可以通过在我的app.module中导入NgbModule.forRoot()来使用。 I am not able to test the component-under-test without importing this module like so: 我不能像这样导入此模块来测试被测组件:

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ComponentUnderTest, TestHostComponent],
      imports: [NgbModule.forRoot()],
      providers: [LocaleService],
    })
      .compileComponents();
  }));

This doesn't seem right, I do not want to test anything from the module. 这似乎不正确,我不想测试模块中的任何内容。

How do I go around importing this module in all my tests? 我如何在所有测试中导入该模块?

I tried using the tooltip in a test project to replicate your error and I didn't hit a problem with the basic tooltip. 我尝试在测试项目中使用工具提示来复制您的错误,但是基本工具提示没有出现问题。 However once I started using [ngbTooltip] it threw an error as it didn't recognise this binding. 但是,一旦我开始使用[ngbTooltip],它便会引发错误,因为它无法识别此绑定。 To get round that you will need to create a mock binding using a mock directive. 为了解决这个问题,您将需要使用模拟指令创建模拟绑定。 The following seemed to do the trick for me: 以下似乎对我有用:

import { Directive, Input } from '@angular/core';

@Directive({
  selector: '[ngbTooltip]'
})
export class MockTooltipDirective {

  @Input()
  ngbTooltip: string;

  constructor() { }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM