2017-02-15 18 views
6

İç içe geçmiş bir bileşende Tooltips ve Modals kullanıyorum ve spec dosyamda, test modülünde NgbModule.forRoot() ithal ediyorum.Testler başarısız olmasına neden olan bileşende NgbModule.forRoot() kullanma

Bu bu bir bileşen dışında her yerde iş gibi görünüyor ve bu ithalat eklerseniz, birimim testlerinin birçok aniden bu hata ile başarısız başlatın:

TypeError: this._unregisterListenersFn is not a function 
     at NgbTooltip.ngOnDestroy 

Ben demetlenmesinde Açısal CLI kullanıyorum/test yapmak.

Bu, testlerimde başarısız olan tek bileşendir.

Ayrıca, Tooltip/Modal modüllerini ayrı olarak ve ilgili sağlayıcılarını ayrı ayrı almayı denedim ve yukarıdaki hatayı alıyorum. forRoot() olmadan denerseniz, DI hatalarını alıyorum.

Sorun nedir hakkında hiçbir fikrim yok.

İşte özellik dosyası var:

/* 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(); 
    }); 
}); 

cevap

1

ben bir çözüm var ama bir test fikstür dahilinde çalışırken bu NgbTooltip ile ilgili bir sorun olduğunu düşünüyorum. NgbTooltip en ngOnDestroy yöntemini yeniden tanımlamak için genel olarak aşağıdaki ekleyin:

NgbTooltip.prototype.ngOnDestroy = function() { 
    this.close(); 
    //this._unregisterListenersFn(); 
    this._zoneSubscription.unsubscribe(); 
}; 

dışarı yorumladı Üçüncü satır benim birim testlerinde görünen hatayı durur. Bir kesmek biraz ama birim testleri tamam olmalı. Bir test donanımında çalışırken bu işlevin ngOnInit() içinde doğru şekilde başlatılmadığını düşünüyorum.

OverrideDirective() ile NgbTooltip yönergesini geçersiz kılmayı denedim, ancak özgün her zaman dikkate alınmayacak gibi görünüyordu.

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

Sadece tavsiye ediyorum:

afterEach(() => { 
    fixture.destroy(); 
}); 

Bu daha sonra meydana gelen gibiydi gerçek istisna görüntülenen:

gerçek hatayı bulmak için benim birim test spec için aşağıdaki eklendi önce önizleme yapın:

// fix 'Error during cleanup of component' 
NgbTooltip.prototype.ngOnDestroy = jasmine.createSpy('ngOnDestroy'); 
(NgbTooltip.prototype.ngOnDestroy as jasmine.Spy).and.stub(); 
İlgili konular