What is Mocking?
Mocking is primarily used in unit testing. An object under test may have dependencies on other (complex) objects. To isolate the behavior of the object you want to test you replace the other objects by mocks that simulate the behavior of the real objects. This is useful if the the real objects are impractical to incorporate into the unit test.
In short, mocking is creating objects that simulate the behavior of real objects.
At times you may want to distinguish between mocking as opposed to stubbing. There may be some disagreement about this subject but my definition of a stub is a "minimal" simulated object. The stub implements just enough behavior to allow the object under test to execute the test.
A mock is like a stub but the test will also verify that the object under test calls the mock as expected. Part of the test is verifying that the mock was used correctly.
To give an example: You can stub a database by implementing a simple in-memory structure for storing records. The object under test can then read and write records to the database stub to allow it to execute the test. This could test some behavior of the object not related to the database and the database stub would be included just to let the test run.
If you instead want to verify that the object under test writes some specific data to the database you will have to mock the database. Your test would then incorporate assertions about what was written to the database mock.
mocking主要用于单元测试。在测试中一个对象可能和其他复杂的对象有关联。那么为了独立这个想测试的对象,那么你就可以使用mock来替换其他的对象来模拟真实对象行为。这个很有用如果真实对象融入到test中是不切实际的话。
mocking和stubbing的区别,stub允许对象在test中执行test。mock和stub很相似但是test会verify test下的object如同预期一样调用了mock。test也verifying mock使用正确。
举例说明:你可以通过执行一个简单的结构体存储记录来stub一个数据库。object可以读取和写入记录到数据库stub中来允许他执行test。这个可以测试一些和数据库不相关的对象并且数据库stub会被包含其中来让test运行。
如果你想确认test中的对象向数据库写入了一些数据,那么你就要mock这个数据库。你的测试然后就会对写入到这个数据库mock给出assertions判断了。
What is Mocking?的更多相关文章
- Testing with a mocking framework (EF6 onwards)
When writing tests for your application it is often desirable to avoid hitting the database. Entity ...
- 单元测试 Mocking 类库需具备的特性
一个优秀的单元测试 Mocking 类库,需要具备如下几个特性: 易用性:有非常明确的 API ,易于使用并易于记忆. 健壮性:行为结果始终一致,并保持准确. 帮助性:当程序出错时,给出尽可能明确的原 ...
- 什么是Mocking framework?它有什么用?
原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why-is-it-useful 今天我想讲下关于mocking fr ...
- Mocking framework
[译] 什么是Mocking framework?它有什么用? 原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why ...
- C# Note37: Writing unit tests with use of mocking
前言 What's mocking and its benefits Mocking is an integral part of unit testing. Although you can run ...
- AngularJS 的异步服务测试与Mocking
测试 AngularJS 的异步服务 最近,在做项目时掉进了 AngularJS 异步调用 $q 测试的坑中,直接躺枪了.折腾了许久日子,终于想通了其中的道道,但并不确定是最佳的解决方案,最后还是决定 ...
- TDD:simply mocking a class is not necessarily the best practice
simply mocking a class is not necessarily the best practice either—it might be better to refactor th ...
- The Art of Mocking
One of the challenges developers face when writing unit tests is how to handle external dependencies ...
- What is a mocking framework? Why is it useful?
Today I want to talk about mocking frameworks and why they are useful. In order to do that I first n ...
随机推荐
- 【转】通过制作Flappy Bird了解Native 2D中的Sprite,Animation
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 引子 上一次我们讲了MonoBehaviou ...
- 实用JS系列——事件类型
事件就是用户对窗口上各种组件的操作.JS中的事件中的事件即由访问Web页面的用户引起的一系列的操作.一般用于浏览器和用户操作进行交互,例如:用户的单击事件等. 类型分为: 内联模型.脚本模型和DOM2 ...
- [BZOJ4205][FJ2015集训] 卡牌配对 [建图+最大流]
题面 这是bzoj权限题,题面可以去下面的离线题库找 离线4205,只有题面,不能提交 思路 二分图匹配 这道题模型显然就是个二分图匹配嘛 那我们两两判断一下然后连边匹配.....就只有30分了 因为 ...
- 真·APIO2018滚粗记
有人说只有大佬才会说滚粗啊爆零啊…… 然而我真滚粗真爆零啊…… D1: 听课,然后夏眠. 咦折纸……哦好吧太神仙了. 咦AI……好妙啊好妙啊. 咦二分……哇还有wqs二分这种神奇操作,学学学. (我是 ...
- 2016"百度之星" - 初赛(Astar Round2A)HDU 5695 拓扑排序+优先队列
Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- CSU 2130~湖南多校对抗第八场 C
2130: Permutations Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: ...
- linux下有的目录有可执行权限
在linux里面,目录也是一个文件.权限设置方法与普通文件一样.目录的可执行权限:目录无所谓执行不执行,目录只能否被开启(也称作“遍历”或“搜索”)对目录的执行权限不仅意味着查看目录下文件名还允许查看 ...
- vue中动态循环model
vue动态循环model与angular有所不同,angular直接定义一个数组,然后传入循环列表的index即可. 而vue不仅需要定义一个数组,还需要通过接口读出循环的数组长度,然后在create ...
- Lesson 7: C#多线程
C#多线程 1.适用于: 通过网络进行通信 执行占用时间的操作 区分具有不同优先级的任务 使用户界面在执行后台任务时能快速响应用户的交互 2.Thread类常用属性及方法 属性: IsAlive:显示 ...
- Binary Indexted Tree 树状数组入门
感谢http://www.cnblogs.com/xudong-bupt/p/3484080.html 树状数组(BIT)是能够完成下述操作的数据结构: 给定一初始值全为零的数列a1,a2a,a3.. ...