日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Angular教程:Angular測試管道

測試管道

你可以在沒有 Angular 測試工具的情況下測試管道。

如果你要試驗(yàn)本指南中所講的應(yīng)用,請?jiān)跒g覽器中運(yùn)行它或下載并在本地運(yùn)行它。

測試 TitleCasePipe

這個管道類有一個方法 ?transform?,它把輸入值變成一個轉(zhuǎn)換后的輸出值。?transform ?的實(shí)現(xiàn)很少會與 DOM 交互。除了 ?@Pipe? 元數(shù)據(jù)和一個接口之外,大多數(shù)管道都不依賴于 Angular。

考慮一個 ?TitleCasePipe?,它會把每個單詞的第一個字母大寫。這里是通過正則表達(dá)式實(shí)現(xiàn)的。

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({name: 'titlecase', pure: true})
/** Transform to Title Case: uppercase the first letter of the words in a string. */
export class TitleCasePipe implements PipeTransform {
  transform(input: string): string {
    return input.length === 0 ? '' :
      input.replace(/\w\S*/g, (txt => txt[0].toUpperCase() + txt.slice(1).toLowerCase() ));
  }
}

任何使用正則表達(dá)式的東西都值得徹底測試。使用簡單的 Jasmine 來探索預(yù)期的案例和邊緣情況。

describe('TitleCasePipe', () => {
  // This pipe is a pure, stateless function so no need for BeforeEach
  const pipe = new TitleCasePipe();

  it('transforms "abc" to "Abc"', () => {
    expect(pipe.transform('abc')).toBe('Abc');
  });

  it('transforms "abc def" to "Abc Def"', () => {
    expect(pipe.transform('abc def')).toBe('Abc Def');
  });

  // ... more tests ...
});

編寫 DOM 測試來支持管道測試

這些都是對管道進(jìn)行隔離測試的。他們無法判斷當(dāng) ?TitleCasePipe ?應(yīng)用于組件中時是否能正常運(yùn)行。

考慮添加這樣的組件測試:

it('should convert hero name to Title Case', () => {
  // get the name's input and display elements from the DOM
  const hostElement: HTMLElement = fixture.nativeElement;
  const nameInput: HTMLInputElement = hostElement.querySelector('input')!;
  const nameDisplay: HTMLElement = hostElement.querySelector('span')!;

  // simulate user entering a new name into the input box
  nameInput.value = 'quick BROWN  fOx';

  // Dispatch a DOM event so that Angular learns of input value change.
  nameInput.dispatchEvent(new Event('input'));

  // Tell Angular to update the display binding through the title pipe
  fixture.detectChanges();

  expect(nameDisplay.textContent).toBe('Quick Brown  Fox');
});

分享標(biāo)題:創(chuàng)新互聯(lián)Angular教程:Angular測試管道
文章分享:http://www.5511xx.com/article/cdhsojg.html