Javascript 如何监视静态生成器功能?

Javascript 如何监视静态生成器功能?,javascript,typescript,jasmine,sequence-generators,Javascript,Typescript,Jasmine,Sequence Generators,我有一个公开生成器的实用函数: 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 <

我有一个公开生成器的实用函数:

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;
        }
    }
}
在我的单元测试(使用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
方法


然而,我不知道如何做到这一点。请建议。

您可以从
实用程序.binaryCombinationGenerator
方法返回jasmine spy对象

let binaryCombinationsSpy=jasmine.createSpyObject('binaryCombinations',['next']);
binaryCombinationsSpy.next.and.returnValues(值1、值2);
spyOn(实用程序,“binaryCombinationGenerator”).和.returnValue(binaryCombinationsSpy);
预期(binaryCombinationsSpy.next)。已被调用时间(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);
//排列
const streamSpy=jasmine.createSpyObj(“myGenerator”、“next”、“counter”);
streamSpy.counter=0;
const values=[{value:here},{value:goes},{value:your},{value:limited},
{value:values}];//如果它是一条无限的溪流,那就设计出别的东西
//为下一步每次调用伪函数
streamSpy.next.and.callFake(()=>{
if(streamSpy.counter
+1谢谢您的回答。然而,为了模仿发电机,我需要更多的东西。我已将我的解决方案作为答案发布。如果你感兴趣的话,可以看看。
//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);