Sinon.spy problems

Sinon.spy problems
0

#1

trying to make a function that imitates forEach, and want to make it so that a spyObject named after the function that I pass my faux-forEach function. So that i can know that it works by looking at how many times the function is called.

forEach = (arr,func)=>{

  sinon.spy((func+'spy'),func);
  //trying to create a spyobject named after the function that we pass forEach

  for(let x = 0; x<arr.length;x++){
    let answer = funct(arr[x]);
    if(answer===false){
      break;
    }
  }
}

From what I understand sinon spies are like objects which store the function as a property

so

sinon.spy( x ,  method)

would create an object called spy which stores a property whose value is the function we are spying on.

so let me try to think how it works using an example

let square = function (n){
return n**2;
}
sinon.spy(squareSpy, square)

this would create an object called ‘squarespy’ which contains the function ‘square’
so

console.log(squareSpy.square)

would return

function (n){
return n**2;
}

So using this Im trying to make it so that creates a similar object for any function that I pass my faux-forEach function so that I can test it is ‘returning’ the correct value / performing the functions on each item in the array , without actually returning anything from the forEach()
this is because the forEach unlike map shouldnt return anything

Maybe someone knows of a good resource to explain how to make and use spies. The documentation is okish, but my understanding is hitting a wall and maybe some simpler more easily demonstrated examples would help

also, whats the point in anonymous functions

fn = sinon.spy()

^this is the example they gave

can I do this and have it work the same

squarespy = sinon.spy(square)


would this do the same thing as

sinon.spy(squareSpy, square)

Overall Im quite confused, because its difficult to test sinon.spy on my software without setting up a whole new environment and messing around with it independently…maybe I should just do that

laziness strikes again!


#2

this is an interesting question/topic. I’m sorry I never heard of the js test library you’re using but it looks like you are going to make something very useful once you get the hang of it…