Snippet · Writing & changing code
Make this testable
If it's hard to test, that's usually telling you something.
I can't easily test this: <paste> Tell me what makes it hard to test — usually it's doing several things at once, or reaching out to the world in the middle of its logic. Then restructure it so the logic can be tested without the network, the clock, the filesystem or the database. Keep the behaviour identical. Show me: - The separated version. - One test that would have been impossible before. - What's still untestable and why that's acceptable.
Difficulty testing is a design signal, not a testing problem. A function that needs six mocks is a function doing six things.