I_L*_*FOO 7 javascript jasmine typescript angular
我有一个带有TypeScript应用程序设置的Angular2,它具有非常基本的Jasmine测试.我想测试一下我的管道.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
在html模板中的用法:
{{size.SizeCode | lpad:['0000']}}
Run Code Online (Sandbox Code Playgroud)
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')
});
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
Error: TypeError: Cannot read property 'length' of undefined(…)
Run Code Online (Sandbox Code Playgroud)
我认为问题在于我的测试的"价值"和"args".知道怎么解决?
这是因为您在使用该describe方法时遇到错误:
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')
});
} // <----------------
Run Code Online (Sandbox Code Playgroud)
您应该使用以下代码:
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')
});
}); // <----------------
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3753 次 |
| 最近记录: |