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 ...
随机推荐
- IDEA 14注册码
用户: share密钥:78689-AFOCD-P3SDN-83DEC-BQ3UC-V6UK7用户: for密钥:13768-8VXX0-YL2BG-YBD88-2M3HN-CAOQ5用户: you密 ...
- [Linux]创建和启用Swap交换区
如果你的服务器的总是报告内存不足,并且时常因为内存不足而引发服务被强制kill的话,在不增加物理内存的情况下,启用swap交换区作为虚拟内存是一个不错的选择,我购买的DigitalOcean VPS ...
- Java 命令行编译项目
如果是用Exlipse, 第三方的包可以放在eclipse文件夹的jre包的lib文件夹中! (初学者的一些总结-高手们勿喷哈-) 原因: 以前一直用Eclispe编程环境运行Java.非常舒服,就像 ...
- [C语言]防止头文件和全局变量重复定义
昨天下午将全局变量定义在H文件中导致链接时提示变量在多个obj文件内重复. 解决办法如下: 将变量移入C文件中进行定义,然后在H文件中加入extern在变量之前. 这样当其它C文件引用该全局变量时 ...
- LeetCode -- Product of Array Except Self My Submissions Question
Question: Given an array of n integers where n > 1, nums, return an array output such that output ...
- CentOS7 安装 webgoat 7.1 简介
CentOS7 安装 webgoat 7.1 简介 webgoat 所需文件准备: 操作系统版本:CentOS 7.3 1: 在Linux上安装Openjdk >= 1.8 2: 上传文件至 L ...
- BZOJ 3932: [CQOI2015]任务查询系统 | 主席树练习题
题目: 洛谷也能评测 题解: De了好长时间BUG发现是自己sort前面有一行for没删,气死. 题目询问第x秒时候前k小的P值之和. 朴素想法: 我们可以把P值离散化,然后对于每个时刻建一棵定义域是 ...
- 2-SAT学习整理
关于2-SAT 问题给出的证明和思路就不再赘述 核心是对于问题给出的条件建图,然后跑tarjan缩点 (在一个强联通分量里bool值是相同的) 看集合两个元素是否在一个强联通分量来判断是否合法 利用强 ...
- sass mixin 持续更新
控制多行显示省略号... //文字溢出省略号@mixin coveText($num:1){ @if $num == 1{ white-space: normal; overflow: hidden; ...
- html状态码
100——客户必须继续发出请求101——客户要求服务器根据请求转换HTTP协议版本 200——交易成功201——提示知道新文件的URL202——接受和处理.但处理未完成203——返回信息不确定或不完整 ...