如何测试angular2动态路由

时间:2016-04-14 13:17:04

标签: testing angular decorator routeconfig

我对angular2测试有一个问题。 我有一些子组件与他们自己的路由,路由配置从服务动态生成,当我尝试测试这个组件我得到错误 不存在的吸气剂(这是正常的)。但是我应该如何以及在哪里将这个吸气剂传递给元件创造过程?

我的组件

import {Component, Input} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES, Router} from 'angular2/router';
/* ------- !Angular 2 native components  ---------*/
import {routeGeneratorInstance} from '../../config/routes/route_instructions_generator';


import {Config} from '../../config/config';
/* ------- !Config  ---------*/

const MODULE_PATH: string = `/${Config.getProdFolderName()}/modules/patients`;

@Component({
  selector: 'patients',
  template: `
    <router-outlet ></router-outlet>
  `,
  directives: [ROUTER_DIRECTIVES]
})

@RouteConfig(routeGeneratorInstance.getRoutesInstructions().patients)

export class PatientsComponent {}

和我的测试

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 {routeGeneratorInstance} from '../../config/routes/route_instructions_generator';
import {PatientsComponent} from './patients.component';

describe('Patients component test', () => {

  it('Should be able to test', injectAsync([TestComponentBuilder],
  (tcb: TestComponentBuilder) => {
    return tcb.createAsync(PatientsComponent).then((componentFixture) => {
      componentFixture.detectChanges();
      expect(true).toBe(true);
    });
  }));

});

得到错误:无法阅读财产&#39;患者&#39;未定义的

据我所知,当我模拟组件creatin时,我没有将此数据传递给RouteConfig,但是如何以及在何处执行此操作?谢谢你的帮助!

0 个答案:

没有答案