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?的更多相关文章

  1. Testing with a mocking framework (EF6 onwards)

    When writing tests for your application it is often desirable to avoid hitting the database.  Entity ...

  2. 单元测试 Mocking 类库需具备的特性

    一个优秀的单元测试 Mocking 类库,需要具备如下几个特性: 易用性:有非常明确的 API ,易于使用并易于记忆. 健壮性:行为结果始终一致,并保持准确. 帮助性:当程序出错时,给出尽可能明确的原 ...

  3. 什么是Mocking framework?它有什么用?

    原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why-is-it-useful 今天我想讲下关于mocking fr ...

  4. Mocking framework

    [译] 什么是Mocking framework?它有什么用? 原位地址:http://codetunnel.com/blog/post/what-is-a-mocking-framework-why ...

  5. 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 ...

  6. AngularJS 的异步服务测试与Mocking

    测试 AngularJS 的异步服务 最近,在做项目时掉进了 AngularJS 异步调用 $q 测试的坑中,直接躺枪了.折腾了许久日子,终于想通了其中的道道,但并不确定是最佳的解决方案,最后还是决定 ...

  7. 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 ...

  8. The Art of Mocking

    One of the challenges developers face when writing unit tests is how to handle external dependencies ...

  9. 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 ...

随机推荐

  1. IPV4的地址是如何分类的?网络号的范围分别是多少?

    1. A类地址 (1)A类地址第1字节为网络地址,其它3个字节为主机地址. (2)A类地址范围:1.0.0.1—126.255.255.254 (3)A类地址中的私有地址和保留地址: ① 10.X.X ...

  2. 【GXZ的原创】平衡树性能测试

    本文作者为 GXZlegend ,转载请注明 出处 ,谢谢! 〇.序言 前些日子闲的蛋疼做了个平衡树性能测试... 主要是因为学会的平衡树越来越多,做题时却不知道写哪个... 本想结合效率和代码复杂度 ...

  3. FreeBSD NTP 简单使用

    FreeBSD NTP 简单使用 来源 https://blog.csdn.net/stevexk/article/details/1349506 1.ntptrace xxx.xxx.xxx.xxx ...

  4. Lights inside 3D Grid LightOJ - 1284 (概率dp + 推导)

    Lights inside 3D Grid LightOJ - 1284 题意: 在一个三维的空间,每个点都有一盏灯,开始全是关的, 现在每次随机选两个点,把两个点之间的全部点,开关都按一遍:问k次过 ...

  5. 雅礼集训 Day2 T3 联盟 解题报告

    联盟 题目描述 \(\text{G}\) 国周边的 \(n\) 个小国家构成一个联盟以抵御 \(\text{G}\) 国入侵, 为互相支援,他们建立了\(n−1\) 条双向通路, 使得任意两个国家可以 ...

  6. 用HTML5绘制的一个星空特效图

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Sed替换文本中的指定内容

    可以这样做:方法1:sed -i 's/被替换的内容/要替换成的内容/' file 方法2:sed 's/被替换的内容/要替换成的内容/g' file > file.outmv file.out ...

  8. ubuntu上安装ffmpeg

      安装包和主要步骤: 1. 首先安装系统基础环境 RHEL & CentOS 系列:yum install -y automake autoconf libtool gcc gcc-c++ ...

  9. c#深拷贝的一个方法

    使用ef,有时候会遇到,要对一个对象进行拷贝复制,可是一般的方法,拷贝后会提示此对象的实例在上下文的 entitystate已经存在,就需要用一种拷贝.简单的拷贝只拷贝了值类型,对引用类型的拷贝需要使 ...

  10. 灰姑娘的水晶鞋(NOIP模拟赛Round 7)

    [问题描述] 传说中的水晶鞋有两种颜色:左边的水晶鞋是红色,右边的是蓝色,据说穿上它们会有神奇的力量. 灰姑娘要找到她所有的n双水晶鞋,它们散落在一条数轴的正半轴上,坐标各不相同,每双水晶鞋还有一个权 ...