首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在angular 7单元测试中测试转换

在angular 7单元测试中测试转换
EN

Stack Overflow用户
提问于 2018-12-14 14:49:42
回答 2查看 857关注 0票数 0

我试图对我的组件进行单元测试,但是我得到了错误--

代码语言:javascript
运行
复制
TypeError: this.oprl10nPipe.transform is not a function

我不能在我的spec文件中模拟转换。

我的组件代码-

代码语言:javascript
运行
复制
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}`;
  }

}

我的元器件规格文件-

代码语言:javascript
运行
复制
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();
  })
});

我试着像这样模拟这个提供,但是它不工作。

代码语言:javascript
运行
复制
{ provide: Oprl10nPipe, useClass: Oprl10nPipeStub },
{ provide: TRANSLATIONS, useValue: translations }

我正在测试我的ngOnChange,这样做是正确的吗?

代码语言:javascript
运行
复制
component.ngOnChanges({
          zoomLevel: new SimpleChange(null, component.zoomLevel, true)
        });

请给我引路。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-19 14:07:53

我已经在我的组件规格文件中添加了这个,它正在工作-

代码语言:javascript
运行
复制
    export class Oprl10nPipeStub {
      public get(key: any): any {
        return of(key);
      }

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

在beforeEach内部-

代码语言:javascript
运行
复制
providers: [
   { provide: Oprl10nPipe, useClass: Oprl10nPipeStub }
],
票数 0
EN

Stack Overflow用户

发布于 2018-12-14 22:05:19

您可以尝试:

代码语言:javascript
运行
复制
const Oprl10nPipeStub = {
  transform: key => of(key)
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53774736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档