<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>进化的测试 &#187; MOCK</title>
	<atom:link href="http://magustest.com/blog/tag/mock/feed/" rel="self" type="application/rss+xml" />
	<link>http://magustest.com/blog</link>
	<description>软件测试，自动化测试，白盒测试，Python</description>
	<lastBuildDate>Wed, 04 Jan 2012 09:09:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>NMock2入门经验</title>
		<link>http://magustest.com/blog/whiteboxtesting/beginning-nmock/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=beginning-nmock</link>
		<comments>http://magustest.com/blog/whiteboxtesting/beginning-nmock/#comments</comments>
		<pubDate>Tue, 22 Apr 2008 19:29:23 +0000</pubDate>
		<dc:creator>magus</dc:creator>
				<category><![CDATA[白盒测试]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[MOCK]]></category>

		<guid isPermaLink="false">http://magustest.com/blog/?p=105</guid>
		<description><![CDATA[每个做单元测试的人都应该一种技术&#8211;MOCK~什么是MOCK？其实没什么神秘的，就是打桩~网上流行的一些.NET的mock框架有：NMock2，Rhino.Mocks等…… NMock2是ThoughtWorks维护的一个开源项目，不知道是不是ThoughtWorks的风格就是敏捷开发基本没有文档，网络上很难找到关于NMock2的文档。而且网上给的例子也很简单，简单到需要自己补充一些东西上去才能跑通。还是用官网上的那个例子：现在有一个银行系统，它实现了一个功能，就是允许两个同名帐户之间实现转账功能。但是很不巧的是，在两个帐户之间转账涉及到汇率的问题，而这个汇率又是天天变的，需要从一个外部的服务来实现的。简单来说就是： 一个接口：IAccountService，实现转账的功能 一个接口：ICurrencyService，实现获取汇率的功能 1 2 3 4 5 6 7 8 9 10 public interface IAccountService &#160;     &#123;          void TransferFunds&#40;Account from, Account to, double amount&#41;;     &#125; &#160;     public interface ICurrencyService     &#123;         double GetConversionRate&#40;string fromCurrency, string toCurrency&#41;;     &#125; 一个类：Account，就是一个一个的帐户 1 2 3 4 5 6 7 8 9 <a href='http://magustest.com/blog/whiteboxtesting/beginning-nmock/'>[...]</a>
Related posts:<ol>
<li><a href='http://magustest.com/blog/whiteboxtesting/refactoring-unit-test/' rel='bookmark' title='测试代码重构实例'>测试代码重构实例</a></li>
<li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
<li><a href='http://magustest.com/blog/webdriver/webdriver-screenshot-on-exception/' rel='bookmark' title='WebDriver测试失败后自动获取截图'>WebDriver测试失败后自动获取截图</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>每个做单元测试的人都应该一种技术&#8211;MOCK~什么是MOCK？其实没什么神秘的，就是打桩~网上流行的一些.NET的mock框架有：NMock2，Rhino.Mocks等……</p>
<p>NMock2是ThoughtWorks维护的一个开源项目，不知道是不是ThoughtWorks的风格就是敏捷开发基本没有文档，网络上很难找到关于NMock2的文档。而且网上给的例子也很简单，简单到需要自己补充一些东西上去才能跑通。还是用官网上的那个例子：现在有一个银行系统，它实现了一个功能，就是允许两个同名帐户之间实现转账功能。但是很不巧的是，在两个帐户之间转账涉及到汇率的问题，而这个汇率又是天天变的，需要从一个外部的服务来实现的。简单来说就是：<br />
<span id="more-105"></span></p>
<p align="justify">一个接口：IAccountService，实现转账的功能<br />
一个接口：ICurrencyService，实现获取汇率的功能</p>
<p align="justify">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IAccountService
&nbsp;
    <span style="color: #008000;">&#123;</span>
         <span style="color: #6666cc; font-weight: bold;">void</span> TransferFunds<span style="color: #008000;">&#40;</span>Account <span style="color: #0600FF; font-weight: bold;">from</span>, Account to, <span style="color: #6666cc; font-weight: bold;">double</span> amount<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> ICurrencyService
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">double</span> GetConversionRate<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> fromCurrency, <span style="color: #6666cc; font-weight: bold;">string</span> toCurrency<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>一个类：Account，就是一个一个的帐户</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Account
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">double</span> _balance<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _name<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span> _currency<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">double</span> balance
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> _balance<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">private</span> set
            <span style="color: #008000;">&#123;</span>
                _balance <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> name
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> _name<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            set
            <span style="color: #008000;">&#123;</span>
                _name <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Currency
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> _currency<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            set
            <span style="color: #008000;">&#123;</span>
                _currency <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Account<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">name</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Currency</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> Account<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> name, <span style="color: #6666cc; font-weight: bold;">string</span> currency<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">name</span> <span style="color: #008000;">=</span> name<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Currency</span> <span style="color: #008000;">=</span> currency<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Withdraw<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> amount<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">balance</span> <span style="color: #008000;">-=</span> amount<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Deposit<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span> amount<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">balance</span> <span style="color: #008000;">+=</span> amount<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>还有一个实现了IAccountService接口的AccountService类</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">   <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> AccountService <span style="color: #008000;">:</span> IAccountService
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> ICurrencyService currencyService<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> AccountService<span style="color: #008000;">&#40;</span>ICurrencyService currencyService<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">currencyService</span> <span style="color: #008000;">=</span> currencyService<span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> TransferFunds<span style="color: #008000;">&#40;</span>Account <span style="color: #0600FF; font-weight: bold;">from</span>, Account to, <span style="color: #6666cc; font-weight: bold;">double</span> amount<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">from</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Withdraw</span><span style="color: #008000;">&#40;</span>amount<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> conversionRate <span style="color: #008000;">=</span> currencyService<span style="color: #008000;">.</span><span style="color: #0000FF;">GetConversionRate</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">from</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Currency</span>, to<span style="color: #008000;">.</span><span style="color: #0000FF;">Currency</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #6666cc; font-weight: bold;">double</span> convertedAmount <span style="color: #008000;">=</span> amount <span style="color: #008000;">*</span> conversionRate<span style="color: #008000;">;</span>
            to<span style="color: #008000;">.</span><span style="color: #0000FF;">Deposit</span><span style="color: #008000;">&#40;</span>convertedAmount<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>这里我们就对ICurrencyService接口实现一个Mock，给测试程序传递一个假的对象</p>
<p>首先在NUnit的代码里面定义需要用到的对象</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        Mockery mocks<span style="color: #008000;">;</span> 
        ICurrencyService mockCurrencyService<span style="color: #008000;">;</span> 
        IAccountService accountService<span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>然后在单元测试的TestInitialize方法里面初始化mock</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        mocks <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mockery<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>  <span style="color: #008080; font-style: italic;">//初始化mocks</span>
        mockCurrencyService <span style="color: #008000;">=</span> mocks<span style="color: #008000;">.</span><span style="color: #0000FF;">NewMock</span><span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span>ICurrencyService<span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//产生一个mock对象</span>
        accountService <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> AccountService<span style="color: #008000;">&#40;</span>mockCurrencyService<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//用mock对象初始化accountService</span></pre></div></div>

<p>最后就是测试的代码：</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ShouldUseCurrencyServiceToDetermineConversionRateBetweenAccounts<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//生成2个帐号</span>
            Account canadianAccount <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Account<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;John Doe&quot;</span>, <span style="color: #666666;">&quot;CAD&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
            Account britishAccount <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Account<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Jane Doe&quot;</span>, <span style="color: #666666;">&quot;GBP&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">//给英国人存100块</span>
            britishAccount<span style="color: #008000;">.</span><span style="color: #0000FF;">Deposit</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Expect<span style="color: #008000;">.</span><span style="color: #0000FF;">Once</span><span style="color: #008000;">.</span><span style="color: #0000FF;">On</span><span style="color: #008000;">&#40;</span>mockCurrencyService<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span>
                Method<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;GetConversionRate&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span>
                With<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;GBP&quot;</span>, <span style="color: #666666;">&quot;CAD&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span>
                Will<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">Return</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2.20</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #008080; font-style: italic;">//对于mockCurrencyService对象，如果他的GetConversionRate方法被调用，而且参数分别是(&quot;GBP&quot;, &quot;CAD&quot;)，那么就返回一个值2.20</span>
&nbsp;
            accountService<span style="color: #008000;">.</span><span style="color: #0000FF;">TransferFunds</span><span style="color: #008000;">&#40;</span>britishAccount, canadianAccount, <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #FF0000;">0</span>, britishAccount<span style="color: #008000;">.</span><span style="color: #0000FF;">balance</span>,<span style="color: #FF0000;">0.00001</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Assert<span style="color: #008000;">.</span><span style="color: #0000FF;">AreEqual</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">double</span><span style="color: #008000;">&#41;</span><span style="color: #FF0000;">220</span>, canadianAccount<span style="color: #008000;">.</span><span style="color: #0000FF;">balance</span>,<span style="color: #FF0000;">0.00001</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            mocks<span style="color: #008000;">.</span><span style="color: #0000FF;">VerifyAllExpectationsHaveBeenMet</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://magustest.com/blog/whiteboxtesting/refactoring-unit-test/' rel='bookmark' title='测试代码重构实例'>测试代码重构实例</a></li>
<li><a href='http://magustest.com/blog/net/automatic-generate-xml-instance-by-using-xsd/' rel='bookmark' title='用XSD自动生成XML对应的.NET实体类'>用XSD自动生成XML对应的.NET实体类</a></li>
<li><a href='http://magustest.com/blog/webdriver/webdriver-screenshot-on-exception/' rel='bookmark' title='WebDriver测试失败后自动获取截图'>WebDriver测试失败后自动获取截图</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://magustest.com/blog/whiteboxtesting/beginning-nmock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

