Error: No provider for Compiler! DI Exception Angular 2 Testing

倾然丶 夕夏残阳落幕 提交于 2019-12-10 17:55:36

问题


The given unit test throws error when executed using npm test command. It says there was a DI Exception with error message "Error: No provider for compiler!"

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);

            });
    });
});

The error is thrown most probably on tcb.createAsync

Following dependencies were added to project

"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:


I think you need

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



来源:https://stackoverflow.com/questions/38470128/error-no-provider-for-compiler-di-exception-angular-2-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!