2016-06-14 3 views
7

아주 기본적인 재스민 테스트로 TypeScript 응용 프로그램 설치로 Angular2가 있습니다. 내 파이프를 테스트하고 싶다. HTML 템플릿에서재 스민이있는 Angular2/TypeScript 파이프 검사

lpad.pipe.ts

import {Pipe, PipeTransform} from '@angular/core'; 
@Pipe({ 
    name: 'lpad' 
}) 
export class LPadPipe implements PipeTransform { 
    transform(value: any, args: string[]): any { 
     let pad = args[0]; 
     return (pad + value).slice(-pad.length); 
    } 
} 

사용 :

{{size.SizeCode | lpad:['0000']}} 

lpad.pipe.spec.ts - 내 시험 (작동하지 않습니다)

import { LPadPipe } from './lpad.pipe'; 

describe('LPadPipe'),() => { 
    let pipe: LPadPipe; 

    beforeEach(() => { 
     pipe = new LPadPipe(); 
    }); 

    it('transforms "1" to "0001"',() => { 
     let value: any = "1"; 
     let args: string[] = ['0000']; 

     expect(pipe.transform(value, args)).ToEqual('0001') 
    }); 

} 

오류 메시지 :

Error: TypeError: Cannot read property 'length' of undefined(…) 

내 테스트의 "가치"와 "args"에 문제가 있다고 생각합니다. 어떤 생각을 어떻게 해결할 수 있습니까? 당신이 오류를 가지고 있기 때문에 describe 방법을 사용하는 경우

답변

8

이있다 :

describe('LPadPipe'),() => { // <---------------- 
    let pipe: LPadPipe; 

    beforeEach(() => { 
    pipe = new LPadPipe(); 
    }); 

    it('transforms "1" to "0001"',() => { 
    let value: any = "1"; 
    let args: string[] = ['0000']; 

    expect(pipe.transform(value, args)).ToEqual('0001') 
    }); 

} // <---------------- 

당신은 대신 다음을 사용한다 :

describe('LPadPipe',() => { // <---------------- 
    let pipe: LPadPipe; 

    beforeEach(() => { 
    pipe = new LPadPipe(); 
    }); 

    it('transforms "1" to "0001"',() => { 
    let value: any = "1"; 
    let args: string[] = ['0000']; 

    expect(pipe.transform(value, args)).ToEqual('0001') 
    }); 

}); // <---------------- 
+3

당신은 100 % 맞아요. 고맙습니다. 나는 또 다른 오타가 있었다 "ToEqual ('0001')"toEqual ('0001') "이어야한다. 이제 작동 중입니다. –

+0

+1 이것은 테스트에서 도움이되었으며, Angular v4 + 파이프가 pipe.transform (value, arg1, arg2, arg3 ....)과 같이 별도로 인수를받을 수 있음을 나타냅니다. –