Unit testing 与angular2 tesing DI的问题

Unit testing 与angular2 tesing DI的问题,unit-testing,testing,dependency-injection,angular,Unit Testing,Testing,Dependency Injection,Angular,我尝试用DI测试一些组件。我正在查找常见的资源,如堆栈/论坛等,但我的问题没有正确的答案(我找不到) 当我试图提供模拟依赖项时,我得到一个错误:必须定义令牌 这是什么?如何提供模拟依赖关系?(在提供的组件中,存在一些下一级依赖项——来自http和config,所以我可以真实地创建它(因为他在没有自己的依赖项的情况下失败了……我想我必须模拟这个依赖项) 这是我的测验 import {provide} from 'angular2/core'; import {setBaseTestProvide

我尝试用DI测试一些组件。我正在查找常见的资源,如堆栈/论坛等,但我的问题没有正确的答案(我找不到)

当我试图提供模拟依赖项时,我得到一个错误:必须定义令牌 这是什么?如何提供模拟依赖关系?(在提供的组件中,存在一些下一级依赖项——来自http和config,所以我可以真实地创建它(因为他在没有自己的依赖项的情况下失败了……我想我必须模拟这个依赖项)

这是我的测验

import {provide} from 'angular2/core';

import {setBaseTestProviders} from 'angular2/testing';
import {
TEST_BROWSER_PLATFORM_PROVIDERS,
TEST_BROWSER_APPLICATION_PROVIDERS
} from 'angular2/platform/testing/browser';

import { beforeEach,beforeEachProviders,describe,expect,
provide,
it,
inject,
injectAsync,
TestComponentBuilder,
AsyncTestCompleter} from 'angular2/testing';


import {HTTPPatientsListService} from '../../shared/http_services/http_patients_list.service';
import {PatientsListComponent} from './patients_list.component';

class MockClass {}

describe('Patients list Tests', () => {
  beforeEachProviders(() => [
    provide(HTTPPatientsListService, {useClass: MockClass})
  ]);

  it('Should defined recentPatientData ', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
    return tcb.createAsync(PatientsListComponent).then((componentFixture: ComponentFixture) => {
      const element = componentFixture.nativeElement;
      componentFixture.detectChanges();
    });
  }));


});
我的部件有一部分(只有一部分,工作正常,买的时间太长)

谢谢你的帮助

注意:错误为:

Chrome 49.0.2623 (Windows 7 0.0.0) Patients list Tests Should defined recentPatientData  FAILED
        Failed: Token must be defined!
        Error: Token must be defined!
            at new BaseException (D:/nucleous/client/src/www/node_modules/angular2/bundles/angular2.dev.js:7521:21)

您需要覆盖测试组件的提供程序

return tcb
.overrideProviders(PatientsListComponent, [provide(HTTPPatientsListService, {useClass: MockClass})])
.createAsync(PatientsListComponent)
.then((componentFixture: ComponentFixture) => {

另请参见

@Günter Zöchbauer的答案是正确的。我还建议检查它是否有非常好的示例。嗨,Gunter。很高兴见到你!我尝试使用你的提示,但出现了新错误:TypeError:tcb.createAsync(…)。overrideProviders不是一个函数它的意思是什么?已解决:)createAsync必须在override providers之后。再次感谢冈特的帮助和时间!返回tcb.overrideProviders(PatientListComponent[提供(HttpPatientListService,{useClass:MockClass})]).createAsync(PatientListComponent)
return tcb
.overrideProviders(PatientsListComponent, [provide(HTTPPatientsListService, {useClass: MockClass})])
.createAsync(PatientsListComponent)
.then((componentFixture: ComponentFixture) => {