javascript - How to spy on static generator functions? -
i have utility function exposes generator:
export class utility { // provides generator streams 2^n binary combinations n variables public static *binarycombinationgenerator(numvars: number): iterableiterator<boolean[]> { (let = 0; < math.pow(2, numvars); i++) { const c = []; //fill c yield c; } } }
now, using generator in code follows:
myfuncion(input){ const n = numberofvariables(input); const binarycombinations = utility.binarycombinationgenerator(n); let combination: boolean[] = binarycombinations.next().value; while (till termination condition met) { // , check whether termination condition met combination = binarycombinations.next().value; } }
in unit tests (using jasmine) want verify how many times generator function invoked (i.e. how many combinations generated) before termination. below have tried:
it("my spec", () => { //arrange const generatorspy = spyon(utility, "binarycombinationgenerator").and.callthrough(); //act //assert expect(generatorspy).tohavebeencalledtimes(16); // fails with: expected spy binarycombinationgenerator have been called 16 times. called 1 times. });
however, assertion fails binarycombinationgenerator
indeed called once. want spy on next
method of iterableiterator
.
however, not sure how that. please suggest.
you return jasmine spy object utility.binarycombinationgenerator
method
let binarycombinationsspy = jasmine.createspyobject('binarycombinations', ['next']); binarycombinationsspy.next.and.returnvalues(value1, value2); spyon(utility, "binarycombinationgenerator").and.returnvalue(binarycombinationsspy); expect(binarycombinationsspy.next).tohavebeencalledtimes(2);
Comments
Post a Comment