zoukankan      html  css  js  c++  java
  • What's the difference between a stub and mock?

    I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They wouldn't do anything fancy and you would have already written the stubbed code for it outside of your test.

    A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.

    Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify. The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.

    I hope that helps.

    reference:

    Mocks Aren't Stubs, posted by Martin Flower,2007.

    What's the difference between a mock & stub? ,stackoverflow

    The art of unit testing.

  • 相关阅读:
    WBS分析
    需求分析-NABCD
    电梯演讲--2048小游戏
    Git diff 算法
    2020 北京理工大学889 回忆
    抖音后台开发社招面试
    珍宝阁
    2018 北京理工大学复试上机 回忆版
    2018 北京理工大学889 回忆
    做一个博客
  • 原文地址:https://www.cnblogs.com/DebugLZQ/p/3160529.html
Copyright © 2011-2022 走看看