为什么有时我会得到“不是已知元素”警告?

发布于 2025-01-27 08:07:19 字数 3121 浏览 5 评论 0原文

我有一项使用业力和茉莉花测试的服务。

title.service.spec

import { TestBed } from '@angular/core/testing';
import { UserPanelComponent } from '../layout/user-panel/user-panel.component';
import { MatExpansionModule } from '@angular/material/expansion';
import { TitleService } from './title.service';

describe('TitleService', () => {
  beforeEach(() => TestBed.configureTestingModule({
    declarations: [ UserPanelComponent ],
    imports: [ MatExpansionModule ],
  }));

  it('should be created', () => {
    const service: TitleService = TestBed.get(TitleService);
    expect(service).toBeTruthy();
  });
});

title.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class TitleService {
 private Value;
    constructor() {
        this.Value = 'shiny beautiful string';
    }
    setValue(val) {
        this.Value = val;
    }
    getValue() {
        return this.Value ;
    }
}

i运行测试,一切都很好。输出:

  TitleService
    √ should be created

我进行了另一项测试,我得到了:

  TitleService
    √ should be created
WARN LOG: ''mat-expansion-panel' is not a known element:
1. If 'mat-expansion-panel' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN: ''mat-expansion-panel' is not a known element:
1. If 'mat-expansion-panel' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN LOG: ''mat-expansion-panel-header' is not a known element:
1. If 'mat-expansion-panel-header' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN: ''mat-expansion-panel-header' is not a known element:
1. If 'mat-expansion-panel-header' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN LOG: ''mat-panel-description' is not a known element:
1. If 'mat-panel-description' is an Angular component, then verify that it is part of this module.
2. If 'mat-panel-description' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN: ''mat-panel-description' is not a known element:
1. If 'mat-panel-description' is an Angular component, then verify that it is part of this module.
2. If 'mat-panel-description' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'

因此,总而言之,在测试时并非每次都会出现警告。为什么?如何永久摆脱它们?

I have a service which I want to test using Karma and Jasmine.

title.service.spec

import { TestBed } from '@angular/core/testing';
import { UserPanelComponent } from '../layout/user-panel/user-panel.component';
import { MatExpansionModule } from '@angular/material/expansion';
import { TitleService } from './title.service';

describe('TitleService', () => {
  beforeEach(() => TestBed.configureTestingModule({
    declarations: [ UserPanelComponent ],
    imports: [ MatExpansionModule ],
  }));

  it('should be created', () => {
    const service: TitleService = TestBed.get(TitleService);
    expect(service).toBeTruthy();
  });
});

title.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class TitleService {
 private Value;
    constructor() {
        this.Value = 'shiny beautiful string';
    }
    setValue(val) {
        this.Value = val;
    }
    getValue() {
        return this.Value ;
    }
}

I run tests and everything is fine. The output:

  TitleService
    √ should be created

I do another testing and I get this:

  TitleService
    √ should be created
WARN LOG: ''mat-expansion-panel' is not a known element:
1. If 'mat-expansion-panel' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN: ''mat-expansion-panel' is not a known element:
1. If 'mat-expansion-panel' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN LOG: ''mat-expansion-panel-header' is not a known element:
1. If 'mat-expansion-panel-header' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN: ''mat-expansion-panel-header' is not a known element:
1. If 'mat-expansion-panel-header' is an Angular component, then verify that it is part of this module.
2. If 'mat-expansion-panel-header' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN LOG: ''mat-panel-description' is not a known element:
1. If 'mat-panel-description' is an Angular component, then verify that it is part of this module.
2. If 'mat-panel-description' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'
WARN: ''mat-panel-description' is not a known element:
1. If 'mat-panel-description' is an Angular component, then verify that it is part of this module.
2. If 'mat-panel-description' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.'

So to sum up, warnings don't appear every time when testing. Why? How to get rid of them permanently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文