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

推荐订阅源

C
Cisco Blogs
Schneier on Security
Schneier on Security
T
Tor Project blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
L
Lohrmann on Cybersecurity
Scott Helme
Scott Helme
Webroot Blog
Webroot Blog
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
Hacker News: Ask HN
Hacker News: Ask HN
PCI Perspectives
PCI Perspectives
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
N
News | PayPal Newsroom
Help Net Security
Help Net Security
The Hacker News
The Hacker News
H
Heimdal Security Blog
O
OpenAI News
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 叶小钗
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security Affairs
P
Proofpoint News Feed
S
Secure Thoughts
腾讯CDC
Google DeepMind News
Google DeepMind News
量子位
罗磊的独立博客

博客园 - NetCobra

近况 让TortoiseSVN使用类似TortoiseCVS的文件冲突解决方式 CSDN双雄——universee(太极语言)和Sinox(汉澳操作系统) The Netron project down, Netron Reloaded... 推荐一个开源项目和一个免费工具 Windows平台下使用Active Directory对Subversion进行权限控制(非完美解决方案) DotNet 项目中链接文件的用处 看看美国的烂公司是怎么样的烂法 SQLite 不能加密? 对软件的多语言化方法的一点看法 最近工作情况 关于 zjsflyer 对 JPgraph 中文显示乱码问题的回复 [旧文]来自 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();