16

I create this code for testing my component.

I tried this code:

describe('Component: AddAlarms', () => {
    let component: AddAlarmsFormComponent;
    let fixture: ComponentFixture<AddAlarmsFormComponent>;
    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [AddAlarmsFormComponent]
        });
        fixture = TestBed.createComponent(AddAlarmsFormComponent);
        component = fixture.componentInstance;
    });
});

when run ng test show this error:

Failed: Template parse errors:
'mat-checkbox' is not a known element:
1. If 'mat-checkbox' is an Angular component, then verify that it is part of this module.
2. If 'mat-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
        </div>
        <div class="input-field col s2">
          [ERROR ->]<mat-checkbox class="example-margin" (click)="sortbydate()">Dates</mat-checkbox>
        </div>
     "): ng:///DynamicTestModule/NotificationsComponent.html@12:10

I verify my module.ts and it's ok. So, I have this:

import {MatCheckboxModule} from '@angular/material/checkbox';

Can you tell me, what is the problem?

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66

2 Answers2

31

You need to add imports array above declarations like this :

Add it like this :

import { MatCheckboxModule } from '@angular/material/checkbox';

And add imports array like this :

TestBed.configureTestingModule({
   imports: [
        MatCheckboxModule
   ],
   declarations: [AddAlarmsFormComponent]
})
Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
0

Great!

But I think you need to import MatCheckboxModule in a spec.ts or you forgot to import the module in "imports" on ngModule!

Can you try and tell me if worked?

Sangwin Gawande
  • 7,658
  • 8
  • 48
  • 66
Chiien
  • 341
  • 1
  • 3
  • 14
  • I decided on both, even on the `module` and in `spec.ts` But nothing happens –  May 21 '18 at 12:14