在角度7单元测试中测试变换

时间:2018-12-14 06:49:42

标签: angular unit-testing karma-jasmine angular7

我正在尝试对组件进行单元测试,但出现错误-

TypeError: this.oprl10nPipe.transform is not a function

我无法在我的规格文件中模拟转换。

我的组件代码-

import { Component, Input, OnInit, OnChanges, SimpleChanges,  ChangeDetectionStrategy} from '@angular/core';
import { DatePipe } from '@angular/common';
import { Severity } from "../../../../../../app-shared/src/lib/types/severity.enum";
import {Oprl10nPipe} from "../../../../../../app-shared/src/lib/l10n-translation/oprl10n.pipe";

@Component({
  selector: 'opr-watchlist-card',
  templateUrl: './watchlist-card.component.html',
  styleUrls: ['./watchlist-card.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class WatchlistCardComponent implements OnInit, OnChanges {

  @Input() cardLabel: string;
  @Input() typeLabel: string;
  @Input() severity: number;
  @Input() lastUpdatedTime: number;
  @Input() isSelected: boolean = false;
  @Input() zoomLevel: number;
  title: string;
  severityName: string;

  constructor(private oprl10nPipe: Oprl10nPipe) { }

  ngOnInit() {

  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes.zoomLevel) {
      this.zoomLevel = changes.zoomLevel.currentValue;
    }
    this.populateTitleData();
  }

  populateTitleData() {
    this.severityName = this.isSeverityNormal == true ? this.oprl10nPipe.transform('opr.watchlist.normal') :
      (this.isSeverityCritical == true ? this.oprl10nPipe.transform('opr.watchlist.critical') :
        (this.isSeverityMajor == true ? this.oprl10nPipe.transform('opr.watchlist.major') :
          (this.isSeverityMinor == true ? this.oprl10nPipe.transform('opr.watchlist.minor') :
            (this.isSeverityWarning == true ? this.oprl10nPipe.transform('opr.watchlist.warning') :
              (this.isSeverityDowntime == true ? this.oprl10nPipe.transform('opr.watchlist.downtime') :
                (this.isSeverityUnknown == true ? this.oprl10nPipe.transform('opr.watchlist.unknown') : ''))))));

    let datePipe = new DatePipe("en-US");
    let displayedLastUpdatedTime = (this.lastUpdatedTime) ? datePipe.transform(this.lastUpdatedTime, 'h:mm a') : '';
    this.title = `[${this.severityName}] ${this.cardLabel}
${this.oprl10nPipe.transform('opr.watchlist.type')} ${this.typeLabel}
${this.oprl10nPipe.transform('opr.watchlist.statusChanged')} ${displayedLastUpdatedTime}`;
  }

}

我的组件规格文件-

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SimpleChange } from '@angular/core';
import { Oprl10nPipe } from "../../../../../../app-shared/src/lib/l10n-translation/oprl10n.pipe";
import { of } from 'rxjs';
import { WatchlistCardComponent } from './watchlist-card.component';
import { TRANSLATIONS } from '@angular/core';

export class Oprl10nPipeStub {
  public get(key: any): any {
    return of(key);
  }
}

const translations = '....';

// export class TranslateServiceStub {
//   public get(key: any): any {
//     return of(key);
//   }
// }

describe('WatchlistCardComponent', () => {
  let component: WatchlistCardComponent;
  let fixture: ComponentFixture<WatchlistCardComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: Oprl10nPipe, useClass: Oprl10nPipeStub },
        { provide: TRANSLATIONS, useValue: translations }
      ],
      declarations: [WatchlistCardComponent]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(WatchlistCardComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

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

  it('should call ngOnChanges', () => {
    component.zoomLevel = 3;
    component.ngOnChanges({
      zoomLevel: new SimpleChange(null, component.zoomLevel, true)
    });
    fixture.detectChanges();
  })
});

我试图像这样嘲笑供应,但是它不起作用。

{ provide: Oprl10nPipe, useClass: Oprl10nPipeStub },
{ provide: TRANSLATIONS, useValue: translations }

我正在测试ngOnChange,因为这样做是正确的方法-

component.ngOnChanges({
          zoomLevel: new SimpleChange(null, component.zoomLevel, true)
        });

请指导我。

2 个答案:

答案 0 :(得分:0)

您可以尝试:

const Oprl10nPipeStub = {
  transform: key => of(key)
};

答案 1 :(得分:0)

我已将此添加到我的组件规格文件中,并且可以正常工作-

    export class Oprl10nPipeStub {
      public get(key: any): any {
        return of(key);
      }

      public transform(key: any): any {
        return of(key);
      }
    }

beforeEach内部-

providers: [
   { provide: Oprl10nPipe, useClass: Oprl10nPipeStub }
],