首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在导致测试失败的组件中使用NgbModule.forRoot()

在导致测试失败的组件中使用NgbModule.forRoot()
EN

Stack Overflow用户
提问于 2017-02-16 05:56:27
回答 2查看 5.3K关注 0票数 6

我在一个嵌套组件中使用了工具提示和模态,在我的规范文件中,我在测试模块中导入了NgbModule.forRoot()

这似乎在任何地方都有效,除了这一个组件,如果我添加这个导入,我的许多单元测试突然开始失败,并出现以下错误:

代码语言:javascript
运行
复制
TypeError: this._unregisterListenersFn is not a function
        at NgbTooltip.ngOnDestroy

我正在使用Angular CLI进行捆绑/测试。

这是我测试中唯一失败的组件。

我还尝试分别导入Tooltip/Modal模块和它们的相关提供程序,但我一直收到上面的错误。如果我在不使用forRoot()的情况下尝试它,我会得到DI错误。

我不知道问题出在哪里。

下面是spec文件:

代码语言:javascript
运行
复制
/* 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();
  });
});
EN

回答 2

Stack Overflow用户

发布于 2017-06-14 13:37:18

我有一个变通方法,但我认为这是NgbTooltip在测试夹具中运行时的问题。全局添加以下内容以重新定义NgbTooltip的ngOnDestroy方法:

代码语言:javascript
运行
复制
NgbTooltip.prototype.ngOnDestroy = function () {
    this.close();
    //this._unregisterListenersFn();
    this._zoneSubscription.unsubscribe();
};

被注释掉的第三行停止了单元测试中出现的错误。这有点小问题,但在单元测试中应该没问题。当在测试夹具中运行时,我认为这个函数在ngOnInit()中没有正确初始化。

我确实尝试过用overrideDirective()覆盖NgbTooltip指令,但是不管怎么说,原始的指令似乎总是被调用的。

为了找出实际的错误,我在单元测试规范中添加了以下内容:

代码语言:javascript
运行
复制
afterEach(() => {
  fixture.destroy();
});

然后,这显示了似乎正在发生的实际异常:

代码语言:javascript
运行
复制
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)
票数 1
EN

Stack Overflow用户

发布于 2017-08-19 02:06:19

我建议在beforeEach中将其存根:

代码语言:javascript
运行
复制
// fix 'Error during cleanup of component'
NgbTooltip.prototype.ngOnDestroy = jasmine.createSpy('ngOnDestroy');
(NgbTooltip.prototype.ngOnDestroy as jasmine.Spy).and.stub();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42260821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档