This might be a dumb question, but is there a good way to test private methods with testbox? Or should I just stick to testing the public stuff and ignore all the in-between steps?
04/15/2022, 5:52 PM
Mock box has a way to make private functions testable but technically your public tests should be good enough testing you private functions. At least in a perfect world. The reason for that is you should be able to completely rewrite your implementation behind the public api of your class and not affect anything including your tests. If your private functions start doing a ton it sometimes means they should be public classes with public functions.
04/15/2022, 6:05 PM
MockBox method in question is makePublic. I use it frequently to test private methods specifically. If you're only testing the public methods you aren't guaranteeing that an individual private method is fulfilling its purpose. A false result could be compensated for by another bug. Kind of a two wrongs make it right situation.
04/15/2022, 6:12 PM
Just started playing around with makePublic and am reading the mock box docs. Everything seems to be working well. Thank you both! We are currently using zero tests at my company, so I am slowly starting to learn how best to implement these tools
04/15/2022, 6:24 PM
One thing I have learned from starting testing at a few places is not to shoot for best. Shoot for getting tests started and teaching the team to follow them and not just ignore failures.
04/15/2022, 9:22 PM
@jumpmaster I would back you up a bit here, and ask "why do you need to test private methods directly"?
That's a bit of an anti-pattern.
But sure, there's sometimes a valid reason for doing so. Worth checking though 😉
(I find I have to sometimes when testing legacy code that was really not written with testing in mind, and the public-facing method is a bit of a mess, and it's difficult to even get to where the private method is called. And refactoring is not always an option)