Angular 2 Component测试错误:无法解析' TestComponentBuilder'

时间:2016-04-06 14:45:01

标签: javascript unit-testing angular

我正在尝试进行Angular 2.0组件测试。我使用this帖子作为参考(在Angular 2.0 dos中没有参考)。

这是我的测试:

import 'reflect-metadata';
import {
  it,
  describe,
  expect,
  inject,
  injectAsync,
  beforeEach,
  beforeEachProviders,
  TestComponentBuilder,
  ComponentFixture
} from 'angular2/testing';
import {Main} from './index';

describe('Main', () => {

  // provide our implementations or mocks to the dependency injector
  beforeEachProviders(() => [
    Main
  ]);

  it('should have title', inject([ Main ], (main) => {
    expect(main.title).toEqual('Hello Angular 2.0');
  }));

  it('should add item to list', injectAsync([TestComponentBuilder, Main], (tcb: TestComponentBuilder, main) => {
    return tcb.createAsync(Main).then((componentFixture: ComponentFixture) => {
      const element = componentFixture.nativeElement;
      expect(element.querySelectorAll('input').length).toBe(1);
    });
  }));
})

现在,我在运行这些测试时遇到Cannot resolve all parameters for 'TestComponentBuilder'错误。尝试将TestComponentBuilder添加到beforeEachProviders结果时出现不同的错误:

Cannot resolve all parameters for 'TestComponentBuilder'(?).

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要设置提供程序以便以这种方式进行测试:

(...)

import {setBaseTestProviders} from 'angular2/testing';

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

describe('Test suite', () => {
  setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS,
                    TEST_BROWSER_APPLICATION_PROVIDERS);

  beforeEach(() => {
    (...)
  });

  (...)
});

以下是一个样本plunkr:https://plnkr.co/edit/oVBPsH?p=preview