“CUSTOM_ELEMENTS_SCHEMA” Errors in Testing Angular 2 App

后端 未结 2 1590
一生所求
一生所求 2021-02-19 01:11

In writing tests for my Angular 2 app, I am running into these errors: the selectors we\'re using:

\"): AppComponent@12:35 \'tab-view\' is not a known element:
1         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 01:31

    So what I had to do to get this working was also set the schemas in the TestBed.configureTestingModule - which is not a separate module file, but a part of the app.component.spec.ts file. Thanks to @camaron for the tip. I do think the docs could be a clearer on this point.

    Anyway, this is what I added to get it to work. Here are the opening contents of my app.component.spec.ts file.

    import { TestBed, async } from '@angular/core/testing';
    import { AppComponent } from './../app.component';
    import { RouterTestingModule } from '@angular/router/testing';
    import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
    
    describe('AppComponent', () => {
      beforeEach(() => {
        TestBed.configureTestingModule({
          schemas: [ CUSTOM_ELEMENTS_SCHEMA ],
          declarations: [AppComponent],
          imports: [RouterTestingModule]
        });
        TestBed.compileComponents();
      });
    

提交回复
热议问题