简体   繁体   English

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

[英]How to spy on static generator functions?

I have an utility function that exposes a generator: 我有一个公开生成器的实用程序函数:

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;
        }
    }
}

Now, I am using this generator in my code as follows: 现在,我在代码中使用此生成器,如下所示:

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;
    }
}

In my unit tests (using Jasmine) I want to verify how many times the generator function is invoked (ie how many combinations are generated) before the termination. 在我的单元测试(使用Jasmine)中,我想验证终止之前调用生成器函数的次数(即生成了多少组合)。 Below is what I have tried: 以下是我尝试过的方法:

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.
});

However, this assertion fails as binaryCombinationGenerator is indeed called once. 但是,此声明失败,因为确实会一次调用binaryCombinationGenerator What I actually want to spy on is the next method of IterableIterator . 我实际上想监视的是IterableIteratornext方法。

However, I am not sure how to do that. 但是,我不确定该怎么做。 Please suggest. 请提出建议。

You could return a jasmine spy object from the Utility.binaryCombinationGenerator method 您可以从Utility.binaryCombinationGenerator方法返回一个茉莉花间谍对象

let binaryCombinationsSpy = jasmine.createSpyObject('binaryCombinations', ['next']);
binaryCombinationsSpy.next.and.returnValues(value1, value2);
spyOn(Utility, "binaryCombinationGenerator").and.returnValue(binaryCombinationsSpy);

expect(binaryCombinationsSpy.next).toHaveBeenCalledTimes(2);

I am posting this as an answer as this is what I have done to mock the generator function. 我将其发布为答案,因为这是我模拟生成器函数所做的。 This is based on @0mpurdy's answer . 这是基于@ 0mpurdy的答案

To mock the generator function, we actually need to call a fake function, which will provide different (and limited, if applicable) values for each call of next() of the generator function. 为了模拟生成器函数,我们实际上需要调用一个伪函数,该函数将为生成器函数的next()的每次调用提供不同的值(并且在适用时受限制)。

This can be simply achieved by the following: 可以通过以下方法简单地实现:

//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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM