Javascript 没有用于TestingCompilerFactory的提供程序
这是我的测试文件,我正在尝试识别阻止我运行成功测试的错误:Javascript 没有用于TestingCompilerFactory的提供程序,javascript,angular,typescript,angular-test,Javascript,Angular,Typescript,Angular Test,这是我的测试文件,我正在尝试识别阻止我运行成功测试的错误: import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { Component, Directive, Input, OnInit } from '@angular/core'; import { TestComponent } from './test.component'; import { NgbDropdownMod
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Component, Directive, Input, OnInit } from '@angular/core';
import { TestComponent } from './test.component';
import { NgbDropdownModule, NgbCollapse } from '@ng-bootstrap/ng-bootstrap';
import { CommonModule } from '@angular/common';
import { Routes } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from '@angular/platform-browser-dynamic/testing';
let comp: TestComponent;
let fixture: ComponentFixture<MyComponent>;
describe('TestComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ TestComponent ],
providers: [
// DECLARE PROVIDERS HERE
{ provide: TestingCompilerFactory }
]
}).compileComponents()
.then(() => {
fixture = TestBed.createComponent(TestComponent);
comp = fixture.componentInstance;
});
}));
it('should be created', () => {
expect(TestComponent).toBeTruthy();
});
但我也有
No provider for TestingCompilerFactory
首先修复语法错误1,2 现在,我们来看一个值得注意的错误 提供者可以采取两种形式 第一个是一个值,它既是提供的值,也是注册它的密钥。这通常采用类的形式,如下例所示
const Dependency = class {};
@NgModule({
providers: [Dependency]
}) export default class {}
第二个是具有provide
属性的对象,该属性指定提供程序注册的密钥,以及一个或多个附加属性,指定所提供的值。一个简单的例子是
const dependencyKey = 'some key';
const Dependency = class {};
@NgModule({
providers: [
{
provide: dependencyKey,
useClass: Dependency
}
]
}) export default class {}
从上面可以推断,您未能指定键TestingCompilerFactory
下提供的实际值
要解决此问题,请编写
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [
{
provide: TestingCompilerFactory,
useClass: TestingCompilerFactory
}
]
})
这是多余的,可以用
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [TestingCompilerFactory]
})
如上所述
注释
我认为这样的语法会阻止您的代码编译,所以完全不清楚您在问什么。您认为语法有什么错误?
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [
{
provide: TestingCompilerFactory,
useClass: TestingCompilerFactory
}
]
})
TestBed.configureTestingModule({
declarations: [TestComponent],
providers: [TestingCompilerFactory]
})