我试图对我的组件进行单元测试,但是我得到了错误--
TypeError: this.oprl10nPipe.transform is not a function我不能在我的spec文件中模拟转换。
我的组件代码-
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)
});请给我引路。
发布于 2018-12-19 14:07:53
我已经在我的组件规格文件中添加了这个,它正在工作-
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 }
],发布于 2018-12-14 22:05:19
您可以尝试:
const Oprl10nPipeStub = {
transform: key => of(key)
};https://stackoverflow.com/questions/53774736
复制相似问题