What is the purpose of mock objects?
Since you say you are new to unit testing and asked for mock objects in "layman's terms", I'll try a layman's example.
Unit Testing
Imagine unit testing for this system:
cook <- waiter <- customer
Its generally easy to envision testing a low-level component like the cook
:
cook <- test driver
The test driver simply orders different dishes and verifies the cook returns the correct dish for each order.
Its harder to test a middle component, like the waiter, that utilizes the behavior of other components. A naive tester might test the waiter component the same way we tested the cook component:
cook <- waiter <- test driver
The test driver would order different dishes and ensure the waiter returns the correct dish. Unfortunately, that means that this test of the waiter component may be dependent on the correct behavior of the cook component. This dependency is even worse if the cook component has any test-unfriendly characteristics, like non-deterministic behavior (the menu includes chef's surprise as an dish), lots of dependencies (cook won't cook without his entire staff), or lot of resources (some dishes require expensive ingredients or take an hour to cook).
Since this a waiter test, ideally, we want to test just the waiter, not the cook. Specifically, we want to make sure the waiter conveys the customer's order to the cook correctly and delivers the cook's food to the customer correctly.
Unit testing means testing units independently, so a better approach would be to isolate the component under test (the waiter) using what Fowler calls test doubles (dummies, stubs, fakes, mocks).
-----------------------
| |
v |
test cook <- waiter <- test driver
Here, the test cook is "in cahoots" with the test driver. Ideally, the system under test is designed so that the test cook can be easily substituted (injected) to work with the waiter without changing production code (e.g. without changing the waiter code).
Mock Objects
Now, the test cook (test double) could be implemented different ways:
- a fake cook - a someone pretending to be a cook by using frozen dinners and a microwave,
- a stub cook - a hot dog vendor that always gives you hot dogs no matter what you order, or
- a mock cook - an undercover cop following a script pretending to be a cook in a sting operation.
See Fowler's article for the more specifics about fakes vs stubs vs mocks vs dummies, but for now, let's focus on a mock cook.
-----------------------
| |
v |
mock cook <- waiter <- test driver
A big part of unit testing the waiter component focuses on how the waiter interacts with the cook component . A mock-based approach focuses on fully specifying what the correct interaction is and detecting when it goes awry.
The mock object knows in advance what is supposed to happen during the test (e.g. which of its methods calls will be invoked, etc.) and the mock object knows how it is supposed to react (e.g. what return value to provide). The mock will indicate whether what really happens differs from what is supposed to happen. A custom mock object could be coded for the expected behavior of each test case, but a mocking framework strives to allow such a behavior specification to be clearly and easily indicated directly in the test case.
The conversation surrounding a mock-based test might look like this:
test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response
test driver (posing as customer) to waiter: I would like a hot dog please
waiter to mock cook: 1 hot dog please
mock cook to waiter: order up: 1 hot dog ready (gives dummy hot dog to waiter)
waiter to test driver: here is your hot dog (gives dummy hot dog to test driver)test driver: TEST SUCCEEDED!
But since our waiter is new, this is what could happen:
test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response
test driver (posing as customer) to waiter: I would like a hot dog please
waiter to mock cook: 1 hamburger please
mock cook stops the test: I was told to expect a hot dog order!test driver notes the problem: TEST FAILED! - the waiter changed the order
or
test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response
test driver (posing as customer) to waiter: I would like a hot dog please
waiter to mock cook: 1 hot dog please
mock cook to waiter: order up: 1 hot dog ready (gives dummy hot dog to waiter)
waiter to test driver: here is your french fries (gives french fries from some other order to test driver)test driver notes the unexpected french fries: TEST FAILED! the waiter gave back wrong dish
It may be hard to clearly see the difference between mock objects and stubs without a contrasting stub-based example to go with this, but this answer is way too long already :-)
Also note that this is a pretty simplistic example and that mocking frameworks allow for some pretty sophisticated specifications of expected behavior from components to support comprehensive tests. There's plenty of material on mock objects and mocking frameworks for more information.
What is the purpose of mock objects?的更多相关文章
- [转载] google mock cookbook
原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...
- [转]TDD之Dummy Stub Fake Mock
TDD之Dummy Stub Fake Mock 测试驱动大家都很熟悉了,这两天正好看了一个java的书,对TDD中的一些基本概念进行了复习,具体如下: Dummy An object that is ...
- Stub, Mock and Proxy Testing
Table of Contents Stubs, Mocks, and Proxies Stub, Mock, and Proxy Testing with Testimonial Mock test ...
- Mock方法介绍
1 现有的单元测试框架单元测试是保证程序正确性的一种有效的测试手段,对于不同的开发语言,通常都能找到相应的单元框架. 借助于这些单测框架的帮助,能够使得我们编写单元测试用例的过程变得便捷而优雅.框架帮 ...
- Rhino Mock
mock interfaces, delegates and classes, including those with parameterized constructors. set expecta ...
- [转载] google mock CheatSheet
原文: https://code.google.com/p/googlemock/wiki/CheatSheet Defining a Mock Class Mocking a Normal Clas ...
- [转载] what's goole mock
原文: https://code.google.com/p/googlemock/wiki/V1_7_ForDummies 地址被墙了, 看起来费劲, 转载一份 Google C++ Mocking ...
- mock的概念
1 现有的单元测试框架单元测试是保证程序正确性的一种有效的测试手段,对于不同的开发语言,通常都能找到相应的单元框架. 借助于这些单测框架的帮助,能够使得我们编写单元测试用例的过程变得便捷而优雅.框架帮 ...
- [转]软件测试- 3 - Mock 和Stub的区别
由于一直没有完全搞明白Mock和Stub的区别,所以查了很多文章,而这一篇是做好的: http://yuan.iteye.com/blog/470418 尤其是8楼,Frostred的发言,描述地相当 ...
随机推荐
- python 的tempfile学习
import os import tempfile print "building a file name yourself:" filename = '/tmp/guess_my ...
- android自定义SlideMenu
完美解决ListView中子项焦点不可被Touch的BUG. 1.在Ecl ...
- win2008服务器信任问题
右键计算机,管理,在第一个页面里面有个安全信息,里面的右边有一个配置IE ESC ,点击他后会出现一个窗口,在那里面选择禁用即可!
- webpack最佳入门实践系列(4)
7.使用字体 7.1.安装字体库-font-awesome 我们通过npm来安装字体 npm install font-awesome --save 这个时候,我们的package.json配置文件变 ...
- 小Z爱图论(NOIP信(sang)心(bin)赛)From FallDream
题目: 小Z最近喜欢上了图论,于是他研究了一下图的连通性问题.但是他遇到了一个难题. 给定一个n个点的有向图,求有多少点对(i,j)满足从i点出发能到达点j ? 小Z仅会简单的朴素算法,所以他想问问你 ...
- iphone CGBitmapContextCreate()函数解释
http://blog.sina.com.cn/s/blog_3e50cef401019cd2.html CGContextRef CGBitmapContextCreate ( void *data ...
- Linux 设备驱动--- 阻塞型字符设备驱动 --- O_NONBLOCK --- 非阻塞标志【转】
转自:http://blog.csdn.net/yikai2009/article/details/8653697 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 阻塞 阻 ...
- C++ 采集音频流(PCM裸流)实现录音功能
与上一篇的“C++ 播放音频流(PCM裸流)” 点击打开链接 相对应,本篇是关于用C++实现录音功能的.同样是直接建一个win32控制台程序然后将代码拷过去改个文件名就可以用,也可以下载本人上传的相关 ...
- c/c++类型转换相关总结
在c语言中存在两种类型转换:显式类型转换和隐式类型转换: 显示类型转换:在类型前加上(type)变量,对变量进行的转换,程序员自己显式添加: char *ptra = (char*)ptrb; voi ...
- Centos 多线程下载工具-axel
32位CentOS执行下面命令: wget -c http://pkgs.repoforge.org/axel/axel-2.4-1.el5.rf.i386.rpm rpm -ivh axel-2.4 ...