[英]Testing Angular2 / TypeScript pipe with Jasmine
I have an Angular2 with TypeScript application setup with very basic Jasmine tests. 我有一个带有TypeScript应用程序设置的Angular2,它具有非常基本的Jasmine测试。 I want to test one of my pipes. 我想测试一下我的管道。
lpad.pipe.ts 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);
}
}
usage in html template: 在html模板中的用法:
{{size.SizeCode | lpad:['0000']}}
lpad.pipe.spec.ts - My test (which doesn't work) 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 message: 错误信息:
Error: TypeError: Cannot read property 'length' of undefined(…)
I think the problem is with the "value" and "args" of my test. 我认为问题在于我的测试的“价值”和“args”。 Any idea how to resolve? 知道怎么解决?
It's because you have errors when using the describe
method: 这是因为使用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')
});
} // <----------------
You should use the following instead: 您应该使用以下代码:
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')
});
}); // <----------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.