| > Counterpoint when using mocks - if Bs behavior changes, one may not remember to update A’s test which would be falsely passing. > This problem is exacerbated if B is a popular object used by many components. This is a great addition to the caveats I mentioned. > IMO if you own A and B, never use mock. I'm going to take issue with your use of the word "never" here. If you said something like, "If you own A and B, don't use mocks the vast majority of the time", I'd even agree--but there are important exceptions (and I'd argue that non-deterministic B is one such exception). > Possibly write a Fake B if B is non deterministic or slow. Then write a parity test for B and Fake B Would you mind explaining what you mean here? |
Perfectly fair. I shouldn’t have stated it as an absolute. If I could still edit it, I would change my statement to “don’t default to a mock”
> Would you mind explaining what you mean here
Gladly. Martin Fowler provides really clear definitions for the different types of test doubles.
https://martinfowler.com/articles/mocksArentStubs.html
Maintaining that your Fakes are correct takes work. An easy way that I’ve found to do that is to run the tests against a “real” component and the Fake component with the exact same set of assertions and set up. If that test breaks, then you know that consuming code should also break