1

I am trying to unit test a module

var foobar = (function() {

  function privateBar() {
    privateBar();
  }

  function privateFoo() {
    privateBar();
  }

  function init()  {
    privateFoo();
  }

  return {
    init: init
  };
}());

How can I use sinon to spy on privateFoo and privateBar to make sure they are called?

spinners
  • 2,449
  • 3
  • 23
  • 34
  • 3
    You don't unit test private methods. Unit testing relies on black box testing. So the answer to your question is, you don't – Liam Jul 22 '16 at 13:06
  • See [Should I test private methods or only public ones?](http://stackoverflow.com/questions/105007/should-i-test-private-methods-or-only-public-ones). In your example init === privateFoo === privateBar so testing the public method will cover all 3. – Liam Jul 22 '16 at 13:08
  • 1
    If I put a letter in the postbox, I just expect it to get to my recipient; I don't care which way the mailman drives nor which machines my letter goes through to make its way to its destination. The same way of thinking is applicable to unit testing. We don't care about the inner workings as long as the input/output is what's expected. – rdiz Jul 22 '16 at 14:16
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures – dm03514 Jul 23 '16 at 19:12

0 Answers0