【问题标题】:Angular Unit testing with Input decorator throw error带有输入装饰器的角度单元测试抛出错误
【发布时间】:2020-01-12 22:25:02
【问题描述】:

这是我运行测试的组件:

import { Component, OnInit, Input } from '@angular/core';

@Component({
    selector: 'shell-bread-crumbs',
    templateUrl: './shell-bread-crumbs.component.html',
    styleUrls: ['./shell-bread-crumbs.component.scss']
})
export class ShellBreadCrumbsComponent implements OnInit {

    @Input() breadCrumb;

    constructor() { }

    ngOnInit() {}

}

这是我的规范文件:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { ShellBreadCrumbsComponent } from './shell-bread-crumbs.component';

describe('ShellBreadCrumbsComponent', () => {

  let component: ShellBreadCrumbsComponent;
  let fixture: ComponentFixture<ShellBreadCrumbsComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ ShellBreadCrumbsComponent ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ShellBreadCrumbsComponent);
    component = fixture.componentInstance;
    component.breadCrumb = ""; //i given empty declaration,
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

但仍然出现错误:

Template parse errors:
    Can't bind to 'breadCrumb' since it isn't a known property of 'bread-crumbs'.
    1. If 'bread-crumbs' is an Angular component and it has 'breadCrumb' input, then verify that it is part of this module.
    2. If 'bread-crumbs' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
    3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("<bread-crumbs [ERROR ->][breadCrumb]="breadCrumb"></bread-crumbs>")

如何解决这个问题?

【问题讨论】:

    标签: angular angular8 angular-unit-test


    【解决方案1】:

    因为你在shell-bread-crumbs.component.html中使用了bread-crumbs

    你可以做两件事

    1. 导入BreadCrumbsModule或在TestBed内声明BreadCrumbsComponent
    beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ 
              ShellBreadCrumbsComponent,
              BreadCrumbsComponent // if you own this component
          ],
          imports: [BreadCrumbsModule] // if you have module
        })
        .compileComponents();
    }));
    

    这样,Angular 将识别bread-crumbs。但是,这可能会产生其他问题。 BreadCrumbsComponent 可能有其他依赖项,并使用您需要声明或定义的其他组件。此外,您可能不关心 BreadCrumbsComponent 进行此测试,这让我们想到了您可以做的第二件事

    1. 使用 CUSTOM_ELEMENTS_SCHEMA 以便 Angular 跳过它无法识别的元素。你可以这样做
    beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ ShellBreadCrumbsComponent ],
          schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
        })
        .compileComponents();
    }));
    

    【讨论】:

    • 我在输入、输出和模拟方面遇到了挑战,我正在使用 ngrx/store 和效果。都需要测试。你能分享我所有的精益教程吗?
    猜你喜欢
    • 2021-09-28
    • 2020-05-28
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2016-12-30
    相关资源
    最近更新 更多