Angular2+ test case indicating lack of NgControl provider

I've been facing an issue while testing an angular component with multiple dependencies. The test case fails with the error: "No Provider for NgControl". Here's the detailed error message:

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

I have already included FormsModule and RouterTestingModule, but the issue persists.

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: [...],
      imports: [...],
      schemas: [NO_ERRORS_SCHEMA]
    }).compileComponents(
    ).then(() => {
        fixture = TestBed.createComponent(ConfiguratorComponent);
        configurator = fixture.componentInstance;
    });
  }));

It seems like there's still a missing module that I need to include in my testing bed setup. Any suggestions on which one I might be overlooking? I'm relatively new to Angular testing environments.

Angular version: 5.2 Karma version: 2.0.2 karma-jasmine version: 1.1.1

Additionally, although it may resemble another unanswered question, I believe adding more context to mine makes it valid. Angular2 testcase shows No provider for NgControl

Answer №1

To better organize responses: At first, you forgot to include ReactiveFormsModule.

As for your second inquiry, remember to include the TranslateStore in the providers array for Testbed.

Answer №2

When setting up your TestBed, be sure to include the necessary imports for FormsModule and/or ReactiveFormsModule.

import { FormsModule, ReactiveFormsModule } from '@angular/forms';

TestBed.configureTestingModule ({
    imports: [FormsModule, ReactiveFormsModule]
});

Reminder: FormsModule is meant for template-driven forms, while ReactiveFormsModule is designed for reactive-driven forms.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improprove performance in Angular by efficiently updating the view through the service

Utilizing the uiSettings service, I am able to control different parts of templates based on user data. Currently, I am directly accessing this service from the template like so: <li *ngIf="uiService.demoButton()"> <button [router ...

Using Angular 2: A Beginner's Guide to Navigating with the Latest Angular 2.0.0-rc.1 Router

As I embarked on a new Angular 2 project, I was puzzled to discover that I inadvertently installed two different versions of the angular router: "@angular/router": "2.0.0-rc.1", "@angular/router-deprecated": "2.0.0-rc.1", Despite my best efforts, I co ...

Struggling to run this unit test (for a helper) smoothly - AngularJS, Karma, Jasmine - Any tips?

As a newcomer to Angular and Karma, the abundance of conflicting advice on how to write unit tests is making things incredibly confusing for me. Any help you can offer would be greatly appreciated! I am currently working on a helper class that relies on a ...

What is the best way to integrate stdlib logging with py.test?

Utilizing py.test to run tests on some modules containing a significant amount of stdlib logging. The goal is for the logging to output to stdout, captured by py.test to display relevant messages in case of test failures. However, an issue arises where th ...

Every time I execute my program, it consistently displays a 500 error message when using both the POST and GET

I'm seeking assistance with mvvm as I am new to it. Can anyone help me in displaying details based on the selected date? Upon running my code, I encounter a 500 error with both the post and get methods. Schedule.cshtml <div class="col-lg-8" ng-ap ...

Load components dynamically and place them in a flexible position based on the context

UPDATE (After gaining a better understanding of the issue): I'm trying to display a component based on where the user clicks (specifically, which table row). Using ng2-smart-table, I've encountered an issue where there isn't a suitable sele ...

Utilize Angular to fetch API and obtain token through a proxy server

I'm curious about how to consume an API that is already published on a website. Let's say the API endpoint is and it returns a JSON with a token like "12jd3..". How can I achieve this from my Angular application? In order to access the API, I co ...

Exploring Query Parameters in Angular 4

Is it possible to generate a custom URL in Angular 4? For instance: http://localhost:4200/#/page/page2/6/abc/1234 Currently, I am utilizing query parameters like this: this.router.navigate(['page/page2/' + row.id],{ queryParams: {'name&a ...

In TypeScript, inferring argument types

Here is the code snippet in question: type Inferred<T> = T extends (...args: (infer UnionType)[]) => any ? UnionType : never function f(first: 'first', second: 'second', bool: boolean) {} type T = Inferred<typeof f> // ...

The Angular user interface is failing to update in accordance with the list obtained from the HTTP GET request made to the .Net Core API

Currently, I am working on an application that utilizes Angular for the frontend and .Net Core API 3.0 for the backend. My objective is to display a list of clients using the Angular Material grid. In my OnInit() method, I invoke my getAllClients service t ...

Extending an interface in TypeScript to include an Array

Can I implement a parent interface in Angular 4? export interface Devices extends Array<Device> { } The error 'Class 'DevicesModel' incorrectly implements interface 'Devices'. Property 'includes' is missing in typ ...

Show the MatCalendar by clicking on a link

Is there a way to trigger the opening of a Material Calendar component using an anchor element? I tried, but the calendar always pops up in the top left corner of the page: <li> <mat-calendar class="hidden"> <input matIn ...

Testing with unittest is a superior option compared to using the assertIn function

Imagine having a lengthy output list that needs to be tested. To do this, you decide to create a sample list with random elements that you believe should also be present in the output list. After consulting the documentation, you attempt to write the follo ...

Is it possible to apply a formatting filter or pipe dynamically within an *ngFor loop in Angular (versions 2 and 4

Here is the data Object within my component sampleData=[ { "value": "sample value with no formatter", "formatter": null, }, { "value": "1234.5678", "formatter": "number:'3.5-5'", }, { "value": "1.3495", "formatt ...

Angular and Ngrx: The optimal approach for choosing a value within a component and a function

While browsing through this Stack Overflow thread, I stumbled upon a question similar to mine. However, I'm curious if the solution provided in the comments is still considered the best practice in today's standards. Here's where I stand: ...

We encountered an unhandled error: It is impossible to assign a value to the property '_showWarnings' of the object '#<Object>'

Looking to implement SSR on my current website. Angular version: v8 Followed the instructions from this link: https://angular.io/guide/universal [error] TypeError: Cannot assign to read only property '_showWarnings' of object '#<Object& ...

Problem with the Auto-fill Feature in PrimeNG Module

Check out my code on Gist:   https://gist.github.com/rickymuvel/8ddc4d14d90877329447ddde9c0aa835 The issue I'm facing involves the Autocomplete module in PrimeNG. It seems that the specific path in the ubigeo.service.ts file is not being called. Her ...

Angular 13's ng serve does not support the keyword "id" for schema ID. Instead, it is recommended to use "$id" to define the schema ID

After successfully upgrading my application from Angular 11 to 13, I encountered some issues while trying to transition from version 12 to 13. Whenever I attempt to run ng serve or ng build, I am faced with the following error: An unhandled exception occ ...

Is there a way to modify the scroll ion-content's color scheme?

Is there a way to change the scroll color in Ionic to green, specifically for ion-content? Any suggestions on how I can achieve this? Below is my HTML code: <ion-header> <ion-toolbar> <ion-buttons slot="start"> ...

Encountering an Unexpected Token in Angular 15, API 29 App

Upon attempting to build and run my Angular application on an Android 10 (API 29) emulator, I encounter a white screen on the device along with the following error message in Android Studio: E/Capacitor/Console: File: http://localhost/ - Line 610 - Msg: Sy ...