2 ms·
Ask HN: A 8LOC unit-test case-study, what would your team do?
If there is a small chunk of code like this in your codebase, how would you (your team) write test for it normally?
function business(n) {
const x = fibonacci(n)
if (x % 2 === 0) {
return moduleA(x)
} else {
return moduleB(x)
}
}
Assumption:
moduleA() and moduleB() are both relying on external system such as DB, web server.
I can see following options while writing test code:
1. Mock moduleA and moduleB, and check whether they were called.
2. Don't mock anything - then write an acceptance test against test db / test web server.
3. Don't write any test for this function specifically because the logic is simple enough.
4. Somehow refactor the code to make it more testable
My own opinion:
Option 1 will introduce coupling between test code and production code implementation.
Option 2 will be slow and might be a waste of effort if moduleA and moduleB have good test coverage already.
Option 3 sounds good to me.
Option 4 how?
Testing methodology is always controversial.
I am curious what your team and you would do in this situation.
Any opinion is appreciated! Cheers