错误:没有编译器的提供程序! DI Exception Angular 2测试

时间:2016-07-19 23:30:12

标签: angular jasmine karma-jasmine

使用npm test命令执行时,给定的单元测试会抛出错误。它说有一个DI异常,错误消息"错误:编译器没有提供者!"

import {TestComponentBuilder} from "@angular/compiler/testing";

import {
    expect,
    it,
    describe,
    async,
    inject,
    beforeEach,
    beforeEachProviders
} from "@angular/core/testing";

import {provide} from '@angular/core';
import {TestService} from "../services/Test.service";
import {TestComponent} from "./Test.component";

describe('Component: TestComponent', () => {

    let tcb;

    beforeEachProviders(() => [
        TestComponentBuilder,
        TestComponent,
        TestService
    ]);

    beforeEach(inject([TestComponentBuilder], (testComponentBuilder) => {
        tcb = testComponentBuilder;
    }));

    it('getTest', testGetTest => {
        **tcb.createAsync(TestComponent)**
            .then(fixture => {
                let TestComponent = fixture.componentInstance;
                TestComponent.selectedUserId = 3;
                expect(TestComponent.selectedUserId).beEqual(3);

            });
    });
});

错误最有可能发生在tcb.createAsync

以下依赖项已添加到项目

"jasmine-core": "~2.4.1",
"karma": "1.x || ^0.13.0",
"karma-chrome-launcher": "1.x || ~0.2.2",
"karma-firefox-launcher": "1.x || ~0.1.7",
"karma-cli": "*",
"karma-jasmine": "1.x || ^0.3.6",
"karma-spec-reporter": "0.0.13",
"browserify": "latest",
"karma-browserify":  "latest" 

1 个答案:

答案 0 :(得分:1)

我认为你需要

import {setBaseTestProviders} from '@angular/core/testing';
import {
  TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
  TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
} from '@angular/platform-browser-dynamic/testing';
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
                     TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

https://github.com/angular/angular/blob/master/CHANGELOG.md#200-rc0-2016-05-02