How can I tell if a function is a sinon spy vs. a stub in code?

84 views
Skip to first unread message

Justin Collum

unread,
Oct 5, 2017, 1:58:02 PM10/5/17
to Sinon.JS
StackOverflow question if anyone would like to tackle this there: https://stackoverflow.com/questions/46061032/how-can-i-tell-if-a-function-is-a-sinon-spy-vs-a-stub

Got a logger that's all stubs. Sometimes I want those stubs to be spies. I can't figure out how to set them back if they are spies.


logger.reset = () => {
  if (!logger.error instanceof sinon.stub) {
    logger.error = sinon.stub();
  }
  if (!logger.fatal instanceof sinon.stub) {
    logger.fatal = sinon.stub();
  }
  for (let level of levels) {
    logger[level].reset();
  }
};


Didn't work. Also tried logger.error.isSinonProxy but the isSinonProxy is true for stubs and spies.

In the meantime I'm just going to re-stub those functions regardless of what type they are. A little wasteful but no big.


But I am curious: how can I tell the difference between a stub and a spy in my code while it's running?

Reply all
Reply to author
Forward
0 new messages