Angular 2 测试用例显示没有 NgControl 的提供者

标签 angular unit-testing typescript testbed

我正在尝试测试一个有很多依赖项的 Angular 组件。但测试用例expect(component).toBeTruthy();失败并显示错误:没有 NgControl 的提供程序。完整的错误消息如下:

Chrome 66.0.3359 (Mac OS X 10.13.4) configurator component unit tests has a configurator FAILED
                    </div>
                    [ERROR ->]<form *ngIf="!loading" class="form" [formGroup]="options">
                        <mat-form-field>
                    <form *ngIf="!loading" class="form" [formGroup]="options">
                        <mat-form-field>
                        <mat-form-field>
                            [ERROR ->]<input matInput #input placeholder="{{'customer.lastname' | translate}}" formControlName="lastname">
                        <mat-form-field>
                            [ERROR ->]<input matInput placeholder="{{'customer.email' | translate}}" formControlName="email">
error properties: Object({ ngSyntaxError: true, ngParseErrors: [ No provider for ControlContainer ("
                            <input matIn"): ng:///DynamicTestModule/CustomerComponent.html@12:5, No provider for NgControl ("
                            [ERROR ->]<input matInput #input placeholder="{{'customer.firstname' | translate}}" formControlName="firstname""): ng:///DynamicTestModule/CustomerComponent.html@14:7, No provider for NgControl ("
"): ng:///DynamicTestModule/CustomerComponent.html@18:7, No provider for NgControl ("
                            <mat-e"): ng:///DynamicTestModule/CustomerC ...
Error: Template parse errors:
    at <Jasmine>
    at syntaxError node_modules/@angular/compiler/esm5/compiler.js:486:22)
    at TemplateParser.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.TemplateParser.parse node_modules/@angular/compiler/esm5/compiler.js:24674:1)
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._parseTemplate node_modules/@angular/compiler/esm5/compiler.js:34629:1)
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileTemplate node_modules/@angular/compiler/esm5/compiler.js:34604:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/esm5/compiler.js:34505:48
    at <Jasmine>
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileComponents node_modules/@angular/compiler/esm5/compiler.js:34505:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/esm5/compiler.js:34393:1
    at Object.then node_modules/@angular/compiler/esm5/compiler.js:475:33)
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileModuleAndAllComponents node_modules/@angular/compiler/esm5/compiler.js:34391:1)
Expected undefined to be truthy.
    at <Jasmine>
    at UserContext.<anonymous> src/app/configurator/configurator.component.spec.ts:90:26)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/proxy.js:128:1)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:387:1)
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 1 of 1 (1 FAILED) (0 secs / 0 secs)
 Chrome 66.0.3359 (Mac OS X 10.13.4) configurator component unit tests has a configurator FAILED
                    </div>
                    [ERROR ->]<form *ngIf="!loading" class="form" [formGroup]="options">
                        <mat-form-field>
                    <form *ngIf="!loading" class="form" [formGroup]="options">
                        <mat-form-field>
                        <mat-form-field>
                            [ERROR ->]<input matInput #input placeholder="{{'customer.lastname' | translate}}" formControlName="lastname">
                        <mat-form-field>
                            [ERROR ->]<input matInput placeholder="{{'customer.email' | translate}}" formControlName="email">
error properties: Object({ ngSyntaxError: true, ngParseErrors: [ No provider for ControlContainer ("
                            <input matIn"): ng:///DynamicTestModule/CustomerComponent.html@12:5, No provider for NgControl ("
                            [ERROR ->]<input matInput #input placeholder="{{'customer.firstname' | translate}}" formControlName="firstname""): ng:///DynamicTestModule/CustomerComponent.html@14:7, No provider for NgControl ("
"): ng:///DynamicTestModule/CustomerComponent.html@18:7, No provider for NgControl ("
                            <mat-e"): ng:///DynamicTestModule/CustomerC ...
Error: Template parse errors:
    at <Jasmine>
    at syntaxError node_modules/@angular/compiler/esm5/compiler.js:486:22)
    at TemplateParser.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.TemplateParser.parse node_modules/@angular/compiler/esm5/compiler.js:24674:1)
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._parseTemplate node_modules/@angular/compiler/esm5/compiler.js:34629:1)
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileTemplate node_modules/@angular/compiler/esm5/compiler.js:34604:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/esm5/compiler.js:34505:48
    at <Jasmine>
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileComponents node_modules/@angular/compiler/esm5/compiler.js:34505:1)
    at http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/compiler/esm5/compiler.js:34393:1
    at Object.then node_modules/@angular/compiler/esm5/compiler.js:475:33)
    at JitCompiler.webpackJsonp../node_modules/@angular/compiler/esm5/compiler.js.JitCompiler._compileModuleAndAllComponents node_modules/@angular/compiler/esm5/compiler.js:34391:1)
Expected undefined to be truthy.
    at <Jasmine>
    at UserContext.<anonymous> src/app/configurator/configurator.component.spec.ts:90:26)
    at ZoneDelegate.webpackJsonp../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke node_modules/zone.js/dist/zone.js:388:1)
    at ProxyZoneSpec.webpackJsonp../node_modules/zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke node_modules/zone.js/dist/proxy.js:128:1)
Chrome 66.0.3359 (Mac OS X 10.13.4): Executed 1 of 1 (1 FAILED) ERROR (0.838 secs / 0 secs)

我已经包含了 FormsModule 和 RouterTestingModule 等内容,但它仍然不起作用。

describe('configurator component unit tests', () => {
  let configurator: ConfiguratorComponent;
  let fixture: ComponentFixture<ConfiguratorComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        ProductsService,
        OrdersService,
        UdiService,
        CustomersService,
        TranslateService,
        {
          provide: ActivatedRoute, useValue: {
            params: Observable.of({ id: 'test' })
          }
        }
      ],
      declarations: [
        ConfiguratorComponent,
        LoginComponent,
        ForgotpasswordComponent,
        ResetpasswordComponent,
        RegisterComponent,
        ContactComponent,
        CustomerComponent,
        HomeComponent,
        PageNotFoundComponent],
      imports: [
        FormsModule,
        CommonModule,
        TranslateModule,
        MatProgressSpinnerModule,
        MatButtonModule,
        ElementsModule,
        ViewerModule,
        OrdersModule,
        RouterTestingModule],
      schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents(
    ).then(() => {
        fixture = TestBed.createComponent(ConfiguratorComponent);
    configurator = fixture.componentInstance;
    });
  }));

所以我显然缺少一些模块,但是是哪一个呢?我对 Angular Testing 床相当陌生。

Angular 版本:5.2 业力版本:2.0.2 业力 Jasmine 版本:1.1.1

哦,它可能与那个重复,但由于在我添加建议后这个问题从未得到答复,我认为这是可以的。 :S Angular2 testcase shows No provider for NgControl

最佳答案

为了帮助巩固答案:最初您缺少ReactiveFormsModule

要回答第二个问题,您需要在 Testbed 的提供者数组中提供 TranslateStore

关于Angular 2 测试用例显示没有 NgControl 的提供者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50606179/

相关文章:

angular - 需要 Angular 7 中的动态复选框列表控件

javascript - 如何用数组值替换字符串中的多个占位符?

javascript - 在 Jasmine 的规范中包含 backbone.js

typescript - 对象不支持属性或方法 'json' servicestack-client HTTP post

typescript - 为什么在遍历同一类型时数组键会发生变化?

angular - 我可以共享一个用于 Web 和 Ionic 的 Angular 项目吗?

javascript - Node.js 上的 Multer 抛出 { 错误 : Unexpected field. .. }

javascript - 获取数组中的每个项目

python - Travis 无法在 Python 3 变体中导入我的测试模块

.NET 相当于 httpunit