TDD how to handle a change in a mocked object

后端 未结 4 1175
孤街浪徒
孤街浪徒 2021-02-13 02:32

In writing unit tests, for each object that the unit interacts with, I am taking these steps (stolen from my understanding of JBrains\' Integration Tests are a Scam):

4条回答
  •  逝去的感伤
    2021-02-13 02:37

    You should not trust human beings (even yourself) about keeping mock and real software components in synch.

    I hear you ask?

    Then what is your proposal?

    My proposal is;

    1. You should write mocks.

    2. You should only write mocks for software components that you maintain.

    3. If you maintain a software component with another developer, you and the other developer should maintain mock of that component together.

    4. You should not mock someone else's component.

    5. When you write a unit test for your component, you should write a separate unit test for the mock of that component. Let's call that MockSynchTest.

    6. In MockSynchTest you should compare every behavior of the mock with the real component.

    7. When you make changes to your component, you should run the MockSynchTest to see if you made your mock and component out-of-synch or not.

    8. If you need the mock of a component that you do not maintain while testing your components, ask the developer of that component about the mock. If she can provide you with a well tested mock, good for her and lucky for you. If she can not, kindly ask her to follow this guideline and provide you a well tested mock.

    This way if you accidentally make your mock out of synch, there will be a failing test case there to warn you.

    This way you do not need to know the implementation details of the foreign component for mocking.

    How-to-write-good-tests#dont-mock-type-you-dont-own

提交回复
热议问题