7
cb219
255d

Every time I try to write a unit test I seem to write an integration test instead. 🤦
I'm just awful at it.

Comments
  • 4
    The code may be making it hard to write good unit tests. Are you using dependency injection? No impure static methods?
  • 1
    @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.
  • 4
    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.
  • 0
    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.
  • 1
    "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.
  • 1
    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
Add Comment