如何监视静态生成器函数?
How to spy on static generator functions?
我有一个公开生成器的实用函数:
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
确实被调用了一次。其实我想窥探的是IterableIterator
的next
方法。
但是,我不确定该怎么做。请推荐。
您可以 return 来自 Utility.binaryCombinationGenerator
方法的 jasmine 间谍对象
let binaryCombinationsSpy = jasmine.createSpyObject('binaryCombinations', ['next']);
binaryCombinationsSpy.next.and.returnValues(value1, value2);
spyOn(Utility, "binaryCombinationGenerator").and.returnValue(binaryCombinationsSpy);
expect(binaryCombinationsSpy.next).toHaveBeenCalledTimes(2);
我将此作为答案发布,因为这是我模拟生成器函数所做的。这是基于 .
为了模拟生成器函数,我们实际上需要调用一个假函数,它将为生成器函数的每次 next()
调用提供不同的(并且有限的,如果适用的话)值。
这可以通过以下方式简单地实现:
//arrange
const streamSpy= jasmine.createSpyObj("myGenerator", ["next", "counter"]);
streamSpy.counter = 0;
const values = [{ value: here }, { value: goes }, { value: your }, { value: limited },
{ value: values }]; // devise something else if it is an infinite stream
// call fake function each time for next
streamSpy.next.and.callFake(() => {
if (streamSpy.counter < values.length) {
streamSpy.counter++;
return values[streamSpy.counter - 1];
}
return { value: undefined }; // EOS
});
spyOn(Utility, "myGenerator").and.returnValue(streamSpy);
...
//assert
expect(streamSpy.next).toHaveBeenCalledTimes(2);
我有一个公开生成器的实用函数:
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
确实被调用了一次。其实我想窥探的是IterableIterator
的next
方法。
但是,我不确定该怎么做。请推荐。
您可以 return 来自 Utility.binaryCombinationGenerator
方法的 jasmine 间谍对象
let binaryCombinationsSpy = jasmine.createSpyObject('binaryCombinations', ['next']);
binaryCombinationsSpy.next.and.returnValues(value1, value2);
spyOn(Utility, "binaryCombinationGenerator").and.returnValue(binaryCombinationsSpy);
expect(binaryCombinationsSpy.next).toHaveBeenCalledTimes(2);
我将此作为答案发布,因为这是我模拟生成器函数所做的。这是基于
为了模拟生成器函数,我们实际上需要调用一个假函数,它将为生成器函数的每次 next()
调用提供不同的(并且有限的,如果适用的话)值。
这可以通过以下方式简单地实现:
//arrange
const streamSpy= jasmine.createSpyObj("myGenerator", ["next", "counter"]);
streamSpy.counter = 0;
const values = [{ value: here }, { value: goes }, { value: your }, { value: limited },
{ value: values }]; // devise something else if it is an infinite stream
// call fake function each time for next
streamSpy.next.and.callFake(() => {
if (streamSpy.counter < values.length) {
streamSpy.counter++;
return values[streamSpy.counter - 1];
}
return { value: undefined }; // EOS
});
spyOn(Utility, "myGenerator").and.returnValue(streamSpy);
...
//assert
expect(streamSpy.next).toHaveBeenCalledTimes(2);