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);
}
}
}
}
查看全文
相关阅读:
当一个模块没有默认导出
<<平仓>>
模态对话框
PlanB S2F 模型
<<深入React技术栈>> CSS Modules
状态提升
ol.proj.transform 坐标系转换
HTMLVideoElement.srcObject MediaStream MediaSource Blob File
毛玻璃特效 backdrop-filter
Filter
原文地址:https://www.cnblogs.com/xhan/p/1494766.html
最新文章
git_info.sh
mac使用备忘录
zookeeper的资料
roundup和ALIGN的对比
simd开发笔记
剩余块用switch处理
Greenplum与Kubernetes
学习docker和Kubernetes
使用hdfs-mount来使hdfs对接greenplum
[Ubuntu] Ubuntu云服务器通过Apache部署Flask
热门文章
[chrome] 扩展插件的使用
win10 tensorflow 1.x 安装
[leetcode]189. 旋转数组
[Git] Git云服务器负责同步
[GitHub]解决图片不显示问题
[HTML+CSS] 好看的登录界面
[JS] 使用JavaScript实现最简单的电子时钟
Ubuntu 18.04 截图默认保存位置修改
MarkDown Latex 数学符号手册(链接)
[汇编语言] win10下的Debug配置
Copyright © 2011-2022 走看看