Mocking vs. Spying in mocking frameworks

后端 未结 7 1287
执念已碎
执念已碎 2020-11-28 20:24

In mocking frameworks, you can mock an object or spy on it. What\'s the difference between the two and when would/should I use one over the other?

7条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 20:55

    Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.

    Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).

    Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

    Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.

    Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

    Mocks Aren't Stubs by Martin Fowler

提交回复
热议问题