Angular 2测试依赖关系

问题描述:

我正在尝试使用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 

问题在于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。