2017-09-28 48 views
0

我有一個angular-cli項目。用phantomjs進行Karma測試失敗,出現ngIf

我要測試的HTML如下:

<div *ngIf="!isMobile" class="shop-description"> 
    {{ description }} 
</div> 
<div class="shop-link"> 
    <span> 
    {{ link}} 
    </span> 
</div> 

測試是這樣的:

beforeEach(() => { 
    fixture = TestBed.createComponent(ShopItemComponent); 
    component = fixture.componentInstance; 
    component.shop = ShopServiceMock.shops[0]; 
    component.isMobile = false; 
    fixture.detectChanges(); 
    }); 
    it('should have a title',() => { 
     fixture.detectChanges(); 
     const el = fixture.debugElement.nativeElement; 
     expect(el.querySelector('div.shop-title>span').innerText).toContain('test'); 
    }); 

    it('should have a description',() => { 
     fixture.detectChanges(); 
     const el = fixture.debugElement.nativeElement; 
     expect(el.querySelector('div.shop-description').innerText).toContain('love'); 
    }); 

命令 'NG測試' 一切經過,沒問題。

隨着「NPM運行試驗:phantomjs - --code覆蓋」上的標題元件穿過所述第一測試,但是,描述的測試失敗,並:

TypeError: null is not an object (evaluating 'el.querySelector('div.shop-description').innerText')

回答

0

我移動component.isMobile = false;內部it('should...和它刪除了錯誤。

我不知道這是爲什麼,但如果有人有解釋,我願意接受。

相關問題