MoQ(基于.net3.5,c#3.0的mock框架)简单介绍
我们在做单元测试的时候,常常困扰于数据的持久化问题,很多情况下我们不希望单元测试影响到数据库中的内容,而且受数据库的影响有时我们的单元测试的速度会很慢,所以我们往往希望将持久化部分隔离开,做单元测试的时候不真正的将数据持久化。这种隔离我们一般使用抽象的方式,也就是利用接口或抽象类将持久化层隔离开,然后利用mock来模拟相应的接口或抽象类来完成相应的持久化类。MoQ就是这种Mock框架之一,MoQ使用了C#3.0,跟NMock相比MoQ使用起来更简单,而且是强类型的方式的,源码和dll可以到http://code.google.com/p/moq/下载。现在MoQ最新的发布版本是3.1版,4.0还处在beta版中,所以我们这里使用的是3.1版。
下面我们就来介绍一下MoQ的具体用法:
一、基础知识
在使用MoQ之前我们必须要先在测试程序中引入Moq.dll,使用MoQ的主要命名空间是Moq,其中最重的类就是Mock<T>,我们可以用这个类来模拟接口。
1、方法
public interface ITest
{
string Test();
}
测试代码:
1 [TestMethod()]
2 public void TestTest()
3 {
4 var test = new Mock<ITest>();
5 test.Setup(p => p.Test()).Returns("lfm");
6 Assert.AreEqual("lfm", test.Object.Test());
7 }
2、匹配参数
public interface IMatchTest
{
string Test(int test);
}
var testMatch = new Mock<IMatchTest>();
testMatch.Setup(p => p.Test(It.Is<int>(i => i % 2 == 0))).Returns("偶数");
testMatch.Setup(p => p.Test(It.Is<int>(i => i % 2 != 0))).Returns("奇数");
Assert.AreEqual("偶数", testMatch.Object.Test(4));
Assert.AreEqual("奇数", testMatch.Object.Test(3));
上边测试代码模拟实现IMathTest接口实例,其中如果Test方法的参数是偶数,其返回值为“偶数”。这里的IT用来过滤参数的类,其具体解释可以参见MoQ的文档
public interface IPropertiesTest
{
int Test { get; set; }
}
var testProperties = new Mock<IPropertiesTest>();
testProperties.Setup(p => p.Test).Returns(1);
Assert.AreEqual(1, testProperties.Object.Test);
或者
var testProperties = new Mock<IPropertiesTest>();
testProperties.SetupProperty(p => p.Test,1);
Assert.AreEqual(1, testProperties.Object.Test);
4、Callback
当执行某方法时调用其内部输入的Action委托
int count = 0;
var testProperties = new Mock<IPropertiesTest>();
testProperties.Setup(p => p.Test).Returns(1).Callback(()=>count++);
Assert.AreEqual(1, testProperties.Object.Test);
Assert.AreEqual(1, count);
在调用Test方法是执行了count++
5、Verification
判断某方法或属性是否执行过
如果代码如下:
1 var testProperties = new Mock<IPropertiesTest>();
2 testProperties.Setup(p => p.Test).Returns(1);
3 testProperties.Verify(p => p.Test);
4 Assert.AreEqual(1, testProperties.Object.Test);
会抛出异常,因为第3行执行时Test方法还没有被调用过,改为如下代码可以通过测试
var testProperties = new Mock<IPropertiesTest>();
testProperties.Setup(p => p.Test).Returns(1); Assert.AreEqual(1, testProperties.Object.Test);
testProperties.Verify(p => p.Test);
其他细节可以查看MoQ文档。
二、应用
先创建一个Account类:
创建一个数据库Provider接口:
public interface ITransferProvider
{ void TransferTo(Account accountFrom, Account accountTo); }
然后创建转账处理类:
1 public class TransferProcess
2 {
3 private Account From;
4 private Account To;
5 private ITransferProvider transfer;
6 public TransferProcess(Account from, Account to, ITransferProvider transfer)
7 {
8 this.From = from;
9 this.To = to;
10 this.transfer = transfer;
11 }
12 public void Transfer(decimal money)
13 {
14 if (money<From.Money)
15 {
16 From.Money = From.Money - money;
17 To.Money = To.Money + money;
18 transfer.TransferTo(From, To);
19 }
20 else
21 {
22 throw new Exception("超出余额");
23 }
24 }
25 }
下边我们来测试这个转账处理类:
var transfer = new Mock<ITransferProvider>();
Account accountFrom = new Account() { AccountNum = 1, Money = 1000, Name = "lfm1" };
Account accountTo = new Account() { AccountNum = 2, Money = 1000, Name = "lfm1" }; TransferProcess tp = new TransferProcess(accountFrom, accountTo, transfer.Object);
tp.Transfer(500);
Assert.AreEqual(500, accountFrom.Money);
Assert.AreEqual(1500, accountTo.Money);
三、参考
http://code.google.com/p/moq/wiki/QuickStart
Beginning Mocking With Moq 3 – Part 1
Beginning Mocking With Moq 3 - Part 2
Beginning Mocking With Moq 3 - Part 3
Beginning Mocking With Moq 3 - Part 4
MoQ(基于.net3.5,c#3.0的mock框架)简单介绍的更多相关文章
- MoQ(基于.net3.5,c#3.0的mock框架)简单介绍(转)
https://www.cnblogs.com/nuaalfm/archive/2009/11/25/1610755.html
- Android(Lollipop/5.0) Material Design(一) 简单介绍
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...
- Hadoop2.6.0子项目hadoop-mapreduce-examples的简单介绍
引文 学习Hadoop的同学们,一定知道如果运行Hadoop自带的各种例子,以大名鼎鼎的wordcount为例,你会输入以下命令: hadoop org.apache.hadoop.examples. ...
- 基于Selenium2+Java的UI自动化(4) - WebDriver API简单介绍
1. 启动浏览器 前边有详细介绍启动三种浏览器的方式(IE.Chrome.Firefox): private WebDriver driver = null; private String chrom ...
- Dubbo学习笔记0:RPC框架Dubbo介绍
整体来说,一个公司业务系统的演进流程基本都是从单体应用到多应用.在单体应用时,不同业务模块相互调用直接在本地JVM进程内就可以完成,而变为多个应用时,相互之间进行通信就不能简单的进行本地调用了,因为不 ...
- Nodejs-express 4.0框架 简单介绍
http://www.expressjs.com.cn/4x/api.html 这个是express API的中文手册 但是只是翻译了一点 英语比较差比较难受. 1. 关于安装 现在网上查的时候有一 ...
- Mock 框架 Moq 的使用
Mock 框架 Moq 的使用 Intro Moq 是 .NET 中一个很流行的 Mock 框架,使用 Mock 框架我们可以只针对我们关注的代码进行测试,对于依赖项使用 Mock 对象配置预期的依赖 ...
- [iOS 基于CoreBluetooth的蓝牙4.0通讯]
一.首先大致介绍下蓝牙4.0的模式,中心和周边: 一般情况下,iPhone作为中心,接收来自周边传感器(比如手环等)采集的数据. 二.那整一个数据通讯的协议是怎样的呢? 为什么要一层层搞这么复杂呢?据 ...
- 用VSCode开发一个基于asp.net core 2.0/sql server linux(docker)/ng5/bs4的项目(1)
最近使用vscode比较多. 学习了一下如何在mac上使用vscode开发asp.netcore项目. 这里是我写的关于vscode的一篇文章: https://www.cnblogs.com/cgz ...
随机推荐
- apache与php安装
安装库文件 yum install -y pcre pcre-devel apr apr-deve 安装apache cd /usr/local/src/httpd-2.4.23 Bundled AP ...
- java 文件的基本操作
1 /** * java 文件操作 * 2016/5/10 **/ package cn.Java_7; import java.io.*; import java.util.Scanner; imp ...
- nmon 安装
安装: mkdir /usr/local/nmon cd /usr/local/nmon wget http://sourceforge.net/projects/nmon/files/nmon_li ...
- 《Windows驱动开发技术详解》之驱动程序的基本结构
驱动对象 每个驱动程序会有唯一的驱动对象与之对应,并且这个驱动对象是在驱动加载的时候被内核中的对象管理程序所创建的.驱动对象用DRIVER_OBJECT数据结构表示,它作为驱动的一个实例被内核加载,并 ...
- python实现邮件发送完整代码(带附件发送方式)
实例一:利用SMTP与EMAIL实现邮件发送,带附件(完整代码) __author__ = 'Administrator'#coding=gb2312 from email.Header import ...
- Shiro 的FilterChain
/** * Shiro的FilterChain * @see ===================================================================== ...
- org.springframework.transaction.CannotCreateTransactionException
HTTP Status 500 - Request processing failed; nested exception is org.springframework.transaction.Can ...
- linux 进程监控和自动重启的简单实现
目的:linux 下服务器程序会因为各种原因dump掉,就会影响用户使用,这里提供一个简单的进程监控和重启功能. 实现原理:由定时任务crontab调用脚本,脚本用ps检查进程是否存在,如果不存在则重 ...
- Sublime console installation instructions install Package Control
instructions: import urllib2,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1 ...
- MFC DLL中导出函数模板
//my.h struct AFX_EXT_CLASS B { }; struct AFX_EXT_CLASS C { }; class AFX_EXT_CLASS A { public: templ ...