3 ms·
Yes I would want the sqrt function to throw an exception if a JSON object was passed instead of a number. Sqrt is only defined for numbers. What would you prefe
by bazoom42 2y ago
Yes I would want the sqrt function to throw an exception if a JSON object was passed instead of a number. Sqrt is only defined for numbers. What would you prefer the code to do in that scenario?
Of course a caller passing a json object to a sqrt function would be in error. This error should be discovered by testing the component which does that.
> The whole point of tests is to ensure your code doesn't trigger runtime errors caused by contract breaches.
IMHO the purpose of tests is to verify that code works correctly. Of course this implies it doesn’t throw unwanted exceptions but that is only a small part of correctness. Sqrt in not correct just because it doesnt throw an exception on a numeric input, it have to return the correct value.
Testing that a component or function works correctly also automatically implies that alle necessary internal contracts are followed. The reverse is not the case.