惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - NetCobra

近况 让TortoiseSVN使用类似TortoiseCVS的文件冲突解决方式 CSDN双雄——universee(太极语言)和Sinox(汉澳操作系统) The Netron project down, Netron Reloaded... 推荐一个开源项目和一个免费工具 Windows平台下使用Active Directory对Subversion进行权限控制(非完美解决方案) DotNet 项目中链接文件的用处 看看美国的烂公司是怎么样的烂法 SQLite 不能加密? - NetCobra 对软件的多语言化方法的一点看法 最近工作情况 关于 zjsflyer 对 JPgraph 中文显示乱码问题的回复 - NetCobra [旧文]来自 Borcon2003 中国开发者大会的现场报道 [问题]DotNet 项目如何实现在构建时 Build 号自动增加? 每日构建中的两个问题 如何防止 Wiki 被恶意篡改? [翻译] NMock 简介 头疼的问题:NAnt 的 cvs-checkout 任务无法执行 可以下载 Delphi 2005 试用版了
[翻译] NMock 两分钟教程
NetCobra · 2004-11-24 · via 博客园 - NetCobra

在 DotText 的代码块上吃了好几次亏,每次都把编辑了半天的帖子给搞丢了,这篇文章就不用代码块了,希望不会有问题。

NMock 两分钟教程


本教程假定你对单元测试和NUnit非常熟悉。

作为一个简单的例子,我们来测试一个信息发布/订阅系统。发布者向零到多个订阅者发布数据,我们需要对发布者进行测试,测试发布者和订阅者之间的交互作用。

public interface ISubscriber
{
    void Receive(Object message);
}

我们将测试发布者对单个的已注册的 ISubscriber 发布信息,为了检测发布者和 ISubscriber 之间的交互作用,测试过程中我们将使用模拟的 ISubscriber。

首先我们来设置测试所需要的环境。首先要创建一个待测试的 Publisher;然后,我们创建一个模拟的 ISubscriber 用于接收信息;接下来把 ISubscriber 在 Publisher 中进行注册,最后,我们来创建一个 message 对象以进行发布。

Mock mockSubscriber = new DynamicMock(typeof(ISubscriber));

Publisher publisher = new Publisher();
publisher.Add((ISubscriber) mockSubscriber.MockInstance);

object message = new Object();

我们所创建的模拟订阅者(mockSubscriber)对象实现了 ISubscriber 接口,这样当测试中的对象调用其 ISubscriber 实例时,我们可以设置断言(assertions)。

下面我们来定义模拟的 ISubscriber 在测试运行过程中需要调用的方法的预期行为(expectations),我们希望 Receive 方法在调用时只传递一个参数——要发送的信息,因为返回值类型为空,因此不需要制定返回值。

mockSubscriber.Expect("Receive",message);

然后执行要测试的代码。

publisher.Publish(message);

最后我们来校验模拟的订阅者是否如我们所期望的被调用了。如果我们不进行校验,那么我们的测试代码将检测到对模拟订阅者的不正确调用,但是不是the complete lack of calls(抱歉不会翻译,原文是our test will detect incorrect calls to the mock Subscriber but not the complete lack of calls.)。

mockSubscriber.Verify();

下面是完整的测试代码: