2016-11-29 140 views
0

我正在嘗試使用Angular 2測試組件,但是在加載依賴時遇到問題。Angular 2測試依賴關係

當我試圖在測試UserShowcaseComponent時嘗試導入UserModule,並且如果我不導入它,我收到很多遺漏的組件錯誤。

測試設置是什麼角CLI生成和我的具體試驗如下

import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 
import { By } from '@angular/platform-browser'; 
import { DebugElement } from '@angular/core'; 

import { UserModule } from '../../user.module'; 
import { UserShowcaseComponent } from './user-showcase.component'; 

describe('UserShowcaseComponent',() => { 
    let component: UserShowcaseComponent; 
    let fixture: ComponentFixture<UserShowcaseComponent>; 

    beforeEach(async(() => { 
     TestBed.configureTestingModule({ 
      imports: [UserModule],  <---------------------------------- NOTE 
     }) 
      .compileComponents(); 
    })); 

    beforeEach(() => { 
     fixture = TestBed.createComponent(UserShowcaseComponent); 
     component = fixture.componentInstance; 
     fixture.detectChanges(); 
    }); 

    it('should create',() => { 
     expect(component).toBeTruthy(); 
    }); 
}); 

從因果報應的錯誤僅僅是這個(這是完整的輸出,沒有測試運行):

29 11 2016 16:49:40.478:INFO [karma]: Karma v1.2.0 server started at http://localhost:9876/ 
29 11 2016 16:49:40.479:INFO [launcher]: Launching browser Chrome with unlimited concurrency 
29 11 2016 16:49:40.649:INFO [launcher]: Starting browser Chrome 
29 11 2016 16:49:42.010:INFO [Chrome 53.0.2785 (Linux 0.0.0)]: Connected on socket /#Jpv1IioGnou6CQtUAAAA with id 98117142 
Chrome 53.0.2785 (Linux 0.0.0) ERROR 

回答

0

問題在於Karma沒有加載供應商文件,而且沒有正確報告。此報告問題已從angular-cli 1.0.0-beta.20修復,請參閱this commit

的解決辦法是增加供應商庫中karma.conf.js,在我的情況下加

{ pattern: './node_modules/jquery/dist/jquery.min.js', watched: false } 

files

是的,我知道我不應該使用jQuery。