Angular 2测试设置

时间:2016-10-10 20:53:51

标签: angular jasmine karma-jasmine angular2-testing

我尝试使用以下设置测试Angular 2组件:

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

import { GroceriesListComponent } from './groceries-list.component';
import { GroceryService }        from './grocery.service';

let comp: GroceriesListComponent;
let fixture: ComponentFixture<GroceriesListComponent>;

beforeEach(() => {
  TestBed.configureTestingModule({
    declarations: [GroceriesListComponent],
    providers:    [GroceryService]
  });

  fixture = TestBed.createComponent(GroceriesListComponent);
  comp = fixture.componentInstance;

  _groceryService = fixture.debugElement.injector.get(GroceryService);

  spy = spyOn(_groceryService, 'getGroceries')
    .and.returnValue(Promise.resolve(testGroceries));

  de = fixture.debugElement.query(By.css('li'));
  el = de.nativeElement;
});

...但我在我的控制台中一直收到以下错误,并且由于缺少信息性错误消息,我对我的设置有什么问题感到遗憾:

404: /base/traceur
ERROR
{
  "originalErr": {}
}

1 个答案:

答案 0 :(得分:0)

这真是一个令人烦恼的错误,我花了一些时间才意识到&#34; traceur&#34;是SystemJS transpiler的默认值。

解决方案是如下调整这两个文件(karma.conf.js和karma-test-shim.js),以便打字稿获取你的转换器:

karma.conf.js

&#13;
&#13;
a = b
&#13;
&#13;
&#13;

果报测试shim.js

&#13;
&#13;
const char* const a = "test";
&#13;
&#13;
&#13;