How can we unit test contract private functions using the Truffle framework?
Since Solidity doesn't have package access or reflection language features, standard private testing approaches don't work.
Creating a test contract which inherits from the contract we're testing seems to be the only approach that works. However these tests would run on the blockchain.
Is there a way to unit test private functions from Javascript?
Best Answer
If they are
internal
functions you can inherit them and test them, if they areprivate
functions, I believe the only way to unit test them is to make thempublic
/internal
, test them and then change them back to private once the tests are passing.Another way to go about it would be to refactor your code so that the private functions are part of a
library
that you import into thecontract
. That way the functions arepublic
in thelibrary
but not exposed in the actualcontract
.