Is there any way to test a nested function (ideally with ScalaTest)?
For example, is there a way to test g()
in the below code:
def f() = {
def g() = "a string!"
g() + "– says g"
}
Is there any way to test a nested function (ideally with ScalaTest)?
For example, is there a way to test g()
in the below code:
def f() = {
def g() = "a string!"
g() + "– says g"
}
g
is not visible outside off
, so I daresay no, at least not without reflection.I think testing
g
would break the concept of unit testing, anyway, because you should never test implementation details but only public API behaviour. Tracking an error to a mistake ing
is part of the debugging process if tests forf
fail.If testing
g
is important for you, defineg
as (protected) method outside off
. That might break your design, though.Another idea would be to put calls to
assert
after the call ofg
in the original code. This will be executed during tests and raise an exception if the property does not hold, causing the test to fail. It will be there in regular code, too, but can be removed by the compiler asassert
(and companions) are elidible (see e.g. here).