简体   繁体   English

没有提供TestingCompilerFactory的提供者

[英]No provider for TestingCompilerFactory

This is my test file and I am trying to identify the error preventing me from running a successful test: 这是我的测试文件,我正在尝试确定导致我无法运行成功测试的错误:

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

I am getting this error which I guess is because I am not wrapping it correctly. 我收到此错误,我猜是因为我没有正确包装它。

error  TS1005: ';' expected. 

But I also get 但我也得到

No provider for TestingCompilerFactory

First fix your syntax error 1,2 首先修复您的语法错误1,2

beforeEach(() => {
  TestBed.configureTestingModule({
    declarations: [TestComponent],
    providers: [{ provide: TestingCompilerFactory }]
  })
  .compileComponents()
  .then(() => {
    fixture = TestBed.createComponent(TestComponent);
    comp = fixture.componentInstance;
  });
}); // excess `)` removed

Now, onto the noteworthy error 现在,值得注意的错误

A provider takes can take two forms. 提供者可以采取两种形式。

The first is a value that acts as both the value provided and the key under which it is registered. 第一个是既用作提供的值又用作注册其值的密钥的值。 This commonly takes the form of a class as in the following example 通常采用类的形式,如以下示例所示

const Dependency = class {};

@NgModule({
  providers: [Dependency]
}) export default class {}

The second is an object with a provide property specifying the key under which the provider is registered and one or more additional properties specifying the value being provided. 第二个对象具有provide属性,该属性指定在其下注册提供程序的密钥,以及一个或多个其他属性,用于指定要提供的值。 A simple example is 一个简单的例子是

const dependencyKey = 'some key';
const Dependency = class {};

@NgModule({
  providers: [
    {
      provide: dependencyKey,
      useClass: Dependency
    }
  ]
}) export default class {}

From the above it you can infer that you failed to specify the actual value provided under the key TestingCompilerFactory . 从上面可以推断出您未能指定在TestingCompilerFactory键下提供的实际值。

To resolve this write 解决此问题

TestBed.configureTestingModule({
  declarations: [TestComponent],
  providers: [
      {
        provide: TestingCompilerFactory,
        useClass: TestingCompilerFactory
      }
    ]
})

which is redundant and can be replaced with 这是多余的,可以替换为

TestBed.configureTestingModule({
  declarations: [TestComponent],
  providers: [TestingCompilerFactory]
})

as described above. 如上所述。

Notes 笔记

  1. In the future do not post questions that include such an obvious error - fix it yourself instead. 以后不要发布包含此类明显错误的问题,请自己解决。

  2. Do not post two questions as one. 不要将两个问题合而为一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM