3 ms·
Of course you can't unit test things with restricted scope. f(x) = x + 2 + 4 How do you unit test x + 2 or (+ 4) even if the operation is pure? You can't. Bec
by ninetyninenine 2y ago
Of course you can't unit test things with restricted scope.
f(x) = x + 2 + 4
How do you unit test x + 2 or (+ 4) even if the operation is pure? You can't. Because it's not callable. It's the same thing with the closure.
The only things that are testable are things on unrestricted scope. AKA global scope. Think about what happens if you have a "closure" on global scope.
If you really want to test it then your "unit tests" which typically live on global scope, need to be moved to local scope. That's just the rules of scope.
There is one special case here. If the parent function returns the local function as a value. But even in this case the parent and local function have to be treated as a unit. The unit test will involve first calling the parent, then calling the local. The parent and child function form a "unit" thanks to shared state and the parent is essentially "moving" the local function into global scope.
Generally best practice is to use combinators if you want to maximize the granularity in which you can modularize your logic. I would even argue that closures stradle the line between pure and impure, so I actually avoid closures whenever possible.