3

I've node App with module with several functions inside and I want to test all the functions inside this module, the problem is that some of them are "private" since I dont export them in the module export command,my question is if it possible to test them? or I should add them to the exports object in order to test them.

I use mocha.

for example this is my code

it('Should update env',
    function (done) {
        var inst = onsr.updatePorts("'[{key:501,key:501}]'", 'key', 10);
        inst.should.equal('[{key:501,key:10}]');
        done();
    })

Here its working since I put the update updatePorts which Is "private" in the module exports but I want to avoid it somehow ...

1 Answers1

0

Don't test the private functions directly, test the public ones that use the private ones.

This will get you coverage on the non-branched lines of code in the private function. In order to get coverage on the remaining lines, you will need to manipulate the inputs and in some cases stub the dependencies in order to simulate situations that exercise those lines of code.

Robbie
  • 18,750
  • 4
  • 41
  • 45
  • I don't think that's a good advice. You want to make sure your code has no bugs, not that your inputs aren't hitting these bugs. A good test suite will ensure all functions are pristine. (*Side not:* that doesn't answer the question) – Amit Aug 16 '15 at 12:22
  • If your inputs aren't hitting those bugs, then the bugs are in redundant code, so you can eliminate the bug by removing the code. - If you test all your private functions directly, you might fix the bugs, but you might not notice that those bugs were in redundant code (i.e. code that cannot be reached via the public api to the system) – Robbie Aug 16 '15 at 12:24
  • No, I disagree. that's not good practice at all. – Amit Aug 16 '15 at 12:28
  • you think its bad practice to only test the parts of the system that can actually be used? why? – Robbie Aug 16 '15 at 12:30
  • I think it's bad practice to not test each and every function in it's own right. Whether or not the code that uses the function causes it to break or not is irrelevant. Doing it the way you suggest would leave deep bugs to be found sometime in the future, and by then it will be hard to locate and potentially difficult to resolve. – Amit Aug 16 '15 at 12:31
  • the users of my code don't tend to complain about bits of code that they can't reach. Also, testing in that way sounds like it might lead you to YAGNI (http://martinfowler.com/bliki/Yagni.html) – Robbie Aug 16 '15 at 12:38
  • http://stackoverflow.com/questions/105007/should-i-test-private-methods-or-only-public-ones – Patrick Aug 16 '15 at 12:39
  • Ohh no... the opposite is true. It leads you to ELIMINATE useless code. It makes sure there are no loose ends. I'm not promoting writing code that isn't used, I'm promoting making sure code that is used is clean of bug, regardless of how it's used. – Amit Aug 16 '15 at 12:40
  • @Patrick - great, you proved us both right ;-) – Amit Aug 16 '15 at 12:43
  • @Amit - lets say you want to refactor your code... a module is getting too big, so you break it up into several smaller modules. If you have direct tests against you're private methods, you've got a lot of changes to make to your tests. In some cases, the existing tests won't even be relevant and will need to be thrown away and possibly replaced. If you test your private code via your public code, then you shouldn't need to change any tests. The fact that your tests still pass after your refactoring tells you that you haven't broken anything that matters (i.e. can be used). – Robbie Aug 16 '15 at 13:03
  • @Robbie there's no real point in arguing as this is a purely opinionated issue. See the link Patrick provided just to see how many different answers there are, and how much support (votes..) each gets. Let's leave it as it is - you think A, I think B, and neither is more correct or wrong then the other. – Amit Aug 16 '15 at 13:08
  • @Patrick I don't appreciate being called a child, particularly not when "taking the higher route" and avoiding the argument. You shouldn't disrespect people like you just did! – Amit Aug 16 '15 at 15:12