What is mocking?

  •                          What is mocking?

    Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to focus on the code being tested and not on the behavior or state of external dependencies. In mocking, the dependencies are replaced by closely controlled replacements objects that simulate the behavior of the real ones. There are three main types of replacement objects - stubs, fakes and mocks.

    Fakes: Fakes replace the actual code by implementing the same interface but without interacting with other objects. The problem Fakes introduce is they are hard coded to return fixed results and in order to test for different use cases a lot of Fakes must be introduced. Tests become hard to understand and maintain since the tests require a lot of Fakes as well as “else” statements to cover all possible statements.

    Stubs: Stubs are similar to Fakes because they return prerecorded answers to calls. However, the difference is that by using a mocking framework you can create the Stub in the test with a minimal amount of code. Thus, it becomes clear how the dependency will respond and how the tested system should behave.

    Mocks: Mocks have all the advantages of Stubs but also provide the option to specify behavior by setting an expectation on how many times a method must be executed. While Stubs are simple substitutes, Mocks are substitutes that can verify usage.

    For more information on the differences between stubs, fakes and mocks read the Fakes, Stubs and Mocks blog post.

    More practical examples on how to get started mocking can be found in the Doing Your First Mock blog post

  • What is a mocking framework?

    Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks. By isolating the dependencies, they help the unit testing process and aid developers in writing more focused and concise unit tests. The tests also perform faster by truly isolating the system under test.

  • What is a test runner?

    Where the unit testing framework adds facilities to create unit tests, the tests still need to be executed to verify the behavior of the system under test. Test Runners execute unit tests. Most of the unit testing frameworks include test runners and they vary from simple command line runners to graphical interfaces.

    Some tools, such as JustCode, a Visual Studio developer productivity tool are able to run many types of unit tests.

    Additional information on the difference between testing frameworks, mocking tools and test runners can be found in the Why Mocking Matters blog post.

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(216)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2017-05-31 在IIS中给某一个网站添加binding的坑
2017-05-31 Firefox中使用pac
2017-05-31 火狐浏览器设置bypass
2017-05-31 如何卸载visualsvn for visual studio
2016-05-31 perl的安装
点击右上角即可分享
微信分享提示