I'm writing some tests for a NodeJS module, but this question can apply for any language / platform. I've got the following situation:
// in moduleA.js
exports.foo = function (var1, var2) {
// do something to var1 and var2 here
baz(var1, var2);
};
exports.bar = function (var1, var2) {
// do something completely different to var1 and var2 here
baz(var1, var2);
};
function baz(var1, var2) {
// do something to var1 and var2 yet again that both foo and bar need
moduleB.doSomething(var1, var2);
}
So, both foo
and bar
are public methods of moduleA
. They've got some specific functionality (they do different things to var1
and var2
) but also some shared functionality (to keep things DRY I've moved it into a private method, baz
) and then they both call a public method on moduleB
.
I've stubbed out moduleB
so I know exactly what's going on after foo
and bar
have been called. I'm writing tests for foo
and bar
's specific functionality but I'm not 100% sure how to approach testing the shared functionality in baz
.
It seems to me I've got three options:
- write identical tests for both
foo
andbar
that test for the things inbaz
- write a single test for
foo
and not write the same test forbar
since know thatbaz
works - expose
baz
as another public method for the module and test it separately.
I'm leaning towards the first option, but I feel I might be missing something.
foo
andbar
. If you're feeling confident, you might feel that a single test for "foo" is enough. Whatever you do, don't testbaz
directly - it's an implementation detail and you don't want to break tests when you refactor it.