Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
The code may be making it hard to write good unit tests. Are you using dependency injection? No impure static methods?
-
cb21910191y@spongessuck static methods don't exist. The codebase wasn't built with testing in mind.
I often don't know whether to mock some code I cannot control or to parameterize it. -
I would refactor what I could to pure functions and give them unit tests. Add acceptance tests elsewhere.
Then start mocking the more complicated side effects one at a time, but don't spend too much time here.
Forcing tests can often lead to highly coupled tests, which makes them less useful for feature adding and refactoring. -
Yeah. Unit tests, by definition, should only test one unit, say, one function, or a set of commonly called sequences.
But agreed, it's a pain when the codebase doesn't lend itself to it. -
"The codebase wasn't built with testing in mind"
...and there's your problem. If it's a convoluted mess of inter-dependencies and spaghetti then it's going to be basically impossible to unit test it effectively. Your options are basically limited to integration tests only, or a massive refactoring. -
OP see "Characterisation tests" from the book 'Working Effectively with Legacy Code'.
Actually, read the entire thing. I'd bet my cousin's goat it would expand your mind
Related Rants
Every time I try to write a unit test I seem to write an integration test instead. 🤦
I'm just awful at it.
rant
unit test
integration test
tdd