我在嵌套组件中使用工具提示和模式,在我的规范文件中,我在测试模块中导入了 NgbModule.forRoot()
。
除了这个组件外,这似乎在任何地方都有效,如果我添加这个导入,我的许多单元测试突然开始失败并出现此错误:
TypeError: this._unregisterListenersFn is not a function
at NgbTooltip.ngOnDestroy
我正在使用 Angular CLI 进行捆绑/测试。
这是我测试失败的唯一组件。
我也尝试过分别导入 Tooltip/Modal 模块及其相关的提供程序,但我一直收到上述错误。如果我在没有 forRoot()
的情况下尝试,我会收到 DI 错误。
我不知道问题是什么。
这是规范文件:
/* tslint:disable:no-unused-variable */
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { RouterTestingModule } from '@angular/router/testing';
import { NgbModule, NgbTooltipModule, NgbTooltipConfig, NgbModalModule } from '@ng-bootstrap/ng-bootstrap';
import { NgbModalStack } from '@ng-bootstrap/ng-bootstrap/modal/modal-stack';
import { ListItemComponent } from './list-item.component';
import { VideoPlayerService } from '../../../video-player';
import { CalendarRoutingService } from '../../calendar-routing.service';
describe('ListItemComponent', () => {
let component: ListItemComponent;
let fixture: ComponentFixture<ListItemComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
ListItemComponent
],
imports: [RouterTestingModule, NgbModule.forRoot()],
providers: [
VideoPlayerService,
CalendarRoutingService,
// NgbModalStack,
// NgbTooltipConfig
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ListItemComponent);
component = fixture.componentInstance;
component.item = { records: [] };
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
最佳答案
我有一个解决方法,但我认为这是在测试夹具中运行时 NgbTooltip 的问题。在全局添加以下内容以重新定义 NgbTooltip 的 ngOnDestroy 方法:
NgbTooltip.prototype.ngOnDestroy = function () {
this.close();
//this._unregisterListenersFn();
this._zoneSubscription.unsubscribe();
};
注释掉的第三行停止了单元测试中出现的错误。有点 hack,但在单元测试中应该没问题。我认为在测试夹具中运行时,此函数未在 ngOnInit() 中正确初始化。
我确实尝试过使用 overrideDirective() 覆盖 NgbTooltip 指令,但不管怎样,原始指令似乎总是被调用。
为了找到实际的错误,我在我的单元测试规范中添加了以下内容:
afterEach(() => {
fixture.destroy();
});
然后显示似乎正在发生的实际异常:
TypeError: this._unregisterListenersFn is not a function
at NgbTooltip.webpackJsonp.../../../../@ng-bootstrap/ng-bootstrap/tooltip/tooltip.js.NgbTooltip.ngOnDestroy (http://localhost:9876/_karma_webpack_/vendor.bundle.js:4522:14)
at callProviderLifecycles (http://localhost:9876/_karma_webpack_/vendor.bundle.js:103669:18)
at callElementProvidersLifecycles (http://localhost:9876/_karma_webpack_/vendor.bundle.js:103638:13)
at callLifecycleHooksChildrenFirst (http://localhost:9876/_karma_webpack_/vendor.bundle.js:103622:17)
at destroyView (http://localhost:9876/_karma_webpack_/vendor.bundle.js:104948:5)
at callViewAction (http://localhost:9876/_karma_webpack_/vendor.bundle.js:105094:13)
at execComponentViewsAction (http://localhost:9876/_karma_webpack_/vendor.bundle.js:105006:13)
at destroyView (http://localhost:9876/_karma_webpack_/vendor.bundle.js:104947:5)
at callViewAction (http://localhost:9876/_karma_webpack_/vendor.bundle.js:105094:13)
at execComponentViewsAction (http://localhost:9876/_karma_webpack_/vendor.bundle.js:105006:13)
关于angular - 在组件中使用 NgbModule.forRoot() 导致测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260821/