zoukankan
html css js c++ java
使用NUnit和DynamicMock进行单元测试
using
System;
using
System.Collections.Generic;
using
System.Text;
using
NUnit.Framework;
using
NUnit.Mocks;
namespace
DynamicMockDemo
{
public
class
Person
{
public
Person(
int
id,
string
name)
{
this
.Id
=
id;
this
.Name
=
name;
}
private
int
id;
public
int
Id
{
get
{
return
id; }
set
{ id
=
value; }
}
private
string
name;
public
string
Name
{
get
{
return
name; }
set
{ name
=
value; }
}
}
public
interface
IDB
{
Person GetPersonById(
int
id);
IList
<
Person
>
GetPersons();
void
DeletePerson(
int
id);
}
[TestFixture]
public
class
Dome
{
[Test]
public
void
Test()
{
Person person1
=
new
Person(
1
,
"
aaa
"
);
Person person2
=
new
Person(
2
,
"
bbb
"
);
IList
<
Person
>
persons
=
new
List
<
Person
>
();
persons.Add(person1);
persons.Add(person2);
//
创建Mock对象
DynamicMock dbMock
=
new
DynamicMock(
typeof
(IDB));
//
模拟GetPersonById方法,传入1,返回person1
dbMock.ExpectAndReturn(
"
GetPersonById
"
, person1,
1
);
//
模拟GetPersons,返回persons
dbMock.SetReturnValue(
"
GetPersons
"
, persons);
//
模拟 DeletePerson,传入3,抛出异常
dbMock.ExpectAndThrow(
"
DeletePerson
"
,
new
Exception(
"
删除失败
"
),
3
);
//
获取IDB接口的Mock对象实例
IDB db
=
(IDB)dbMock.MockInstance;
Person testPerson
=
db.GetPersonById(
1
);
Assert.AreEqual(person1.Name, testPerson.Name);
IList
<
Person
>
testPersons
=
db.GetPersons();
Assert.AreEqual(
2
, testPersons.Count);
try
{
db.DeletePerson(
3
);
}
catch
(Exception e)
{
Assert.AreEqual(
"
删除失败
"
, e.Message);
}
}
}
}
查看全文
相关阅读:
动态生成表格 (ng-zorro)
单例服务
模板变量 #
HTML 5 系列
关于tcp nagle算法
netty 解包头包体的一点认知
vargent Authentication failure.记录
关于YII2.0配置的一点问题
关于mysql b-tree索引的一点认知
记vagrant nginx sendfile问题
原文地址:https://www.cnblogs.com/xhan/p/1494766.html
最新文章
spyder快捷键
输出带颜色的文字
类和函数-学习笔记
类和对象
python
cJSON使用说明
makefile中的特殊符号及关键字
python 正则表达式
python 线程与进程
python IO编程
热门文章
python 错误、调试和测试
Python 偏函数 partial function
python基础练习
第六章 核心API (二)
ng组件样式
ng在父子组件 | 指令之间 共享数据
所有的类型,去重
js表单校验API
js表单_校验表单字段是否为空
post请求(下载文件)
Copyright © 2011-2022 走看看