Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

6
  • 7
    This "unit tests are the contract" point is a critical one I'm not sure I've seen made explicitly enough elsewhere. Commented Jun 15, 2023 at 2:27
  • 2
    "I've almost never had to make a mock of an interface for a unit test." You must have a different concept of unit test than I do. If you test a "unit" (a class, a module, some small-ish part of code) in isolation (because this is what a unit test does: It disregards the rest of the software): Then you have to, by definition, replace -- mock, stub, whatever -- the parts of the software with which the unit is interfacing. Commented Jun 15, 2023 at 12:31
  • @Peter: In many cases, units can be designed with minimal dependencies, so that there are few if any other pieces of code that need to be faked. This is why one of my bullet points was "To discourage unnecessary coupling." The anticipated pain of writing unit tests for code that has lots of coupling would hopefully guide the design towards more isolated, standalone units. Also, I believe that, if unit Foo depends on some specialty container and that container is itself thoroughly unit tested, then there's no point in mocking the container when testing Foo. Commented Jun 15, 2023 at 16:14
  • 1
    @Corrodias: Absolutely. Any object that acts as an interface to a database session, network connection, clock, etc., probably needs a mock or fake of that thing. But there should be very few of those. I've seen too many designs where every unit that processes data is directly dependent on the data source and thus all of those units need dependency injection so the test can pass in a fake/mock. In nearly all cases, those units should just accept data directly from the caller. Commented Jun 15, 2023 at 18:44
  • 4
    "Unit tests are the contract" ... In the case of changed requirement : first change the test. Run the testsuite : the unmodified code should fail the test. Then you are in a strong position to modify the code to implement the changed requirement. Commented Jun 15, 2023 at 21:40