【发布时间】:2021-07-09 12:43:53
【问题描述】:
我是单元测试的新手,我需要帮助在 karma 中为 Sort Pipe 编写 TestCase。我的自定义排序管道正在调用基于DirectionValue的方法,其中排序的逻辑是。
@Pipe({
name: 'sortBy'
})
export class SortByPipe implements PipeTransform {
transform(value[], direction:string){
if (direction === 'desc'){
this.descendingSort(value);
}
else if(direction === 'asc'){
this.ascendingSort(value);
}
return value;
}
第一种情况:想检查是否是基于方向值的Calling right Method。
第二种情况:检查排序结果。
我试图为第二种情况写这样的东西。
const direction = 'asc';
const pipe = new SortByPipe();
const result = pipe.transform([4,3,2,1], direction);
expect(result).toBe([1,2,3,4]);
请帮助我了解如何解决它。 提前致谢
【问题讨论】:
-
有什么错误?
-
最初是
Expected [ 4, 3, 2, 1 ] to be [ 1, 2, 3, 4 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().这个Expected [ 4, 3, 2, 1 ] to be [ 1, 2, 3, 4 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().而不是我将toBe 更改为toEqual 然后它给出了以下错误,这是显而易见的。Error: Expected $[0] = 4 to equal 1. Expected $[1] = 3 to equal 2. Expected $[2] = 2 to equal 3. Expected $[3] = 1 to equal 4
标签: angular karma-jasmine angular-unit-test