我希望我的Angular 组件应该从CommonModule中名为TitleCaseTube.

在我的测试文件下面:

test.component.ts

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { CommonModule } from '@angular/common';

@Component({
  template: `<span>{{ text | titlecase }}</span>`,
  standalone: true,
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [CommonModule]
})
export class TestComponent {
  @Input() text: string;
}

test.component.spec.ts

import { ChangeDetectionStrategy } from '@angular/core';
import { CommonModule, TitleCasePipe } from '@angular/common';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TestComponent } from './test.component';

describe('TestComponent', () => {
  let component: TestComponent;
  let fixture: ComponentFixture<TestComponent>;
  let titleCasePipeSpy: any = jasmine.createSpyObj('TitleCasePipe', ['transform']);

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [TestComponent, CommonModule],
      providers: [{ provide: TitleCasePipe, useValue: titleCasePipeSpy }],
    }).overrideComponent(TestComponent, {
      set: { changeDetection: ChangeDetectionStrategy.Default }
    }).compileComponents();

    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should render text using titlecase', () => {
    component.test= 'test';
    fixture.detectChanges();

    expect(titleCasePipeSpy.transform).toHaveBeenCalledTimes(1);
  });
});

我的测试结果:"预期调用了一次spy TitleCasePipe.Transform.它被调用了0次".我在这里错过了什么?

推荐答案

给出一个肯定会被titleCase管道转换的输入,这样你就可以在html中判断输出,而不是判断在被测试组件之外执行的函数!逻辑在内部以Angular 发生,因此您无法判断transform方法是否已被调用!

import { ChangeDetectionStrategy } from '@angular/core';
import { CommonModule, TitleCasePipe } from '@angular/common';
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { TestComponent } from './test.component';
import { By } from '@angular/platform-browser';

describe('TestComponent', () => {
  let component: TestComponent;
  let fixture: ComponentFixture<TestComponent>;
  let titleCasePipeSpy: any = jasmine.createSpyObj('TitleCasePipe', [
    'transform',
  ]);

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [TestComponent, CommonModule],
      providers: [{ provide: TitleCasePipe, useValue: titleCasePipeSpy }],
    })
      .overrideComponent(TestComponent, {
        set: { changeDetection: ChangeDetectionStrategy.Default },
      })
      .compileComponents();

    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should render text using titlecase', () => {
    component.text = 'hello world';
    fixture.detectChanges();

    expect(
      fixture.debugElement.query(By.css('span')).nativeElement.textContent
    ).toContain('Hello World');
  });
});

stackblitz->;cd test->;npm i->;ng test判断输出!

Angular相关问答推荐

如何避免使用CSS动画制作的幻灯片中闪烁?

RXJS运算符用于combineLatest,不含空值

try 测试具有Angular material 下拉列表的组件时,在Angular 17中发现&q;错误的意外合成属性@TransitionMessages

如何在HTMLTITLE属性中使用Angular 显示特殊字符?

仅在展示时使用垫式步进器

如何通过innerHtml在模板中显示动态插入的动感图标?

将sass与@Use语句一起使用时出现Angular 新的VITE构建器编译错误

Angular-每2个流式传输多个HTTP调用

如何检测 Angular Universal 预渲染版本?

Angular6:RxJS switchMap 运算符未按预期工作

Angular2:更新数组时*ngFor不更新

如何在 Angular 2 模板中使用枚举

如何以Angular 2 获取当前路由自定义数据?

Angular 2:Validators.pattern() 不工作

如何在 Angular 2 应用程序中配置不同的开发环境

Angular 6 - NullInjectorError:单元测试中没有 HttpClient 的提供者

在同一个组件中使用 MatSort 的多个 mat-table

如何在 Angular CLI 的构建时插入内部版本号或时间戳

如何作为模块的子模块路由到模块 - Angular 2 RC 5

Angular2动态改变CSS属性