TDD how to handle a change in a mocked object

后端 未结 4 2353
我寻月下人不归
我寻月下人不归 2021-02-13 02:33

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:46

    I do it this way.

    Suppose I have to change the responses from interface method foo(). I gather all the collaboration tests that stub foo() in a list. I gather all the contract tests for method foo(), or if I don't have contract tests, I gather all the tests for all the current implementations of foo() in a list.

    Now I create a version control branch, because it'll be messy for a while.

    I @Ignore (JUnit speak) or otherwise disable the collaboration tests that stub foo() and start re-implementing and re-running them one by one. I get them all passing. I can do this without touching any production implementation of foo().

    Now I re-implement the objects that implement foo() one by one with expected results that match the new return values from the stubs. Remember: stubs in collaboration tests correspond to expected results in contract tests.

    At this point, all the collaboration tests now assume the new responses from foo() and the contract tests/implementation tests now expect the new responses from foo(), so It Should All Just Work.(TM)

    Now integrate your branch and pour yourself some wine.

提交回复
热议问题