代码之家  ›  专栏  ›  技术社区  ›  Javascript Coder

角7单元测试中的测试变换

  •  0
  • Javascript Coder  · 技术社区  · 7 年前

    我试图对我的组件进行单元测试,但我发现了一个错误-

    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 回复  |  直到 7 年前
        1
  •  0
  •   pzaenger lio    7 年前

    const Oprl10nPipeStub = {
      transform: key => of(key)
    };
    
        2
  •  0
  •   Javascript Coder    7 年前

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

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

    在每个之前都在里面-

    providers: [
       { provide: Oprl10nPipe, useClass: Oprl10nPipeStub }
    ],
    
    推荐文章