我有一个公开生成器的实用程序函数:
export class Utility {
// provides a generator that streams 2^n binary combinations for n variables
public static *binaryCombinationGenerator(numVars: number): IterableIterator<boolean[]> {
for (let i = 0; i < Math.pow(2, numVars); i++) {
const c = [];
//fill up c
yield c;
}
}
}
现在,我在我的代码中使用这个生成器,如下所示:
myFuncion(input){
const n = numberOfVariables(input);
const binaryCombinations = Utility.binaryCombinationGenerator(n);
let combination: boolean[] = binaryCombinations.next().value;
while (till termination condition is met) {
// do something and check whether termination condition is met
combination = binaryCombinations.next().value;
}
}
在我的单元测试(使用 Jasmine)中,我想验证在终止之前调用生成器函数的次数(即生成了多少组合)。以下是我尝试过的:
it("My spec", () => {
//arrange
const generatorSpy = spyOn(Utility, "binaryCombinationGenerator").and.callThrough();
//act
//assert
expect(generatorSpy).toHaveBeenCalledTimes(16); // fails with: Expected spy binaryCombinationGenerator to have been called 16 times. It was called 1 times.
});
然而,这个断言失败了,因为binaryCombinationGenerator
它确实被调用了一次。我真正想窥探的next
是IterableIterator
.
但是,我不知道该怎么做。请建议。