<?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>Infinity &#187; Moq</title>
	<atom:link href="http://blog.cyragon.com/tag/moq/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cyragon.com</link>
	<description>where it all begins</description>
	<lastBuildDate>Fri, 11 Feb 2011 20:26:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Extending Moq&#8217;s Setup() to return multiple values</title>
		<link>http://blog.cyragon.com/2010/06/extending-moqs-setup-to-return-multiple-values/</link>
		<comments>http://blog.cyragon.com/2010/06/extending-moqs-setup-to-return-multiple-values/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 20:37:35 +0000</pubDate>
		<dc:creator>jwcarroll</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Moq]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://blog.cyragon.com/?p=134</guid>
		<description><![CDATA[If you regularly use TDD, then you are familiar with the concept of mocking. If you don&#8217;t use TDD, then you need to slap yourself and apologize to Bob Martin, Michael Feathers and Ron Jeffries immediately. I&#8217;ll wait here&#8230; While I would argue you should begin your TDD journey by writing your own mocks; using a framework [...]]]></description>
			<content:encoded><![CDATA[<p>If you regularly use <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a>, then you are familiar with the concept of mocking. If you don&#8217;t use TDD, then you need to slap yourself and apologize to <a href="http://www.objectmentor.com/omTeam/martin_r.html">Bob Martin</a>, <a href="http://www.objectmentor.com/omTeam/feathers_m.html">Michael Feathers</a> and <a href="http://www.objectmentor.com/omTeam/jeffries_r.html">Ron Jeffries</a> immediately. I&#8217;ll wait here&#8230;</p>
<p>While I would argue you should begin your TDD journey by <a href="http://blog.objectmentor.com/articles/2009/10/28/manual-mocking-resisting-the-invasion-of-dots-and-parentheses">writing your own mocks</a>; using a framework will certainly make like easier in many instances and allow you to focus on the tests themselves.</p>
<p>I was a <a href="http://www.ayende.com/projects/rhino-mocks.aspx">RhinoMocks</a> guy for a long time, but have recently converted to <a href="http://code.google.com/p/moq/">Moq</a>&#8230; AND I LOVE IT!!! Moq is simple and easy to use, and doesn&#8217;t get in the way of the intent of your tests. That being said, there are certain scenarios you run into with any framework that you end up coding around, and sometimes that <em>does</em> get in the way of your what you trying to say with your test.</p>
<p>Moq allows for a pretty simple syntax when you want to set up a return value on a mocked type.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code9'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1349"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p134code9"><pre class="csharp" style="font-family:monospace;">var mockService <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
mockService<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">SomeMethodCall</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Hello, World!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>
Pretty sweet so far, and that works 99% of the time. But&#8230; what about a scenario where we want to return different values on successive calls to that method. I can think of a lot of situations where, in the absence of formal eventing, you want your code to test some value until it meets a certain criteria. You might be polling a web service for status updates, or checking a database to see if a record has been written. Whatever the case may be, it <em>will</em> come up.</p>
<p>So, for illustration purposes, let&#8217;s make up one such example. Suppose we wanted to ask a service for a specified set of values, but the return types could be varied. Perhaps we only care about some subset of those values, and want to test that our code correctly deals with variable data.</p>
<p>We have our service contract:<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code10'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13410"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p134code10"><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> IReturnStuff
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">String</span> ReturnAString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
Next we have our consuming class (the subject under test):<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code11'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13411"><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
</pre></td><td class="code" id="p134code11"><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> UsesReturnedStuff
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> IReturnStuff ReturnStuff <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">private</span> Regex Filter <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> UsesReturnedStuff<span style="color: #008000;">&#40;</span>IReturnStuff returnStuff<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        ReturnStuff <span style="color: #008000;">=</span> returnStuff<span style="color: #008000;">;</span>
        Filter <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Regex<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;(the|quick|brown|fox|hello|world)(:Pu)?&quot;</span>, RegexOptions<span style="color: #008000;">.</span><span style="color: #0000FF;">IgnoreCase</span><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> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">&gt;</span> GetStuff<span style="color: #008000;">&#40;</span>Int32 times<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        var stuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> times<span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var newStuff <span style="color: #008000;">=</span> ReturnStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>Filter<span style="color: #008000;">.</span><span style="color: #0000FF;">IsMatch</span><span style="color: #008000;">&#40;</span>newStuff<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                stuff<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>newStuff<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;">return</span> stuff<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
As you can see we have a pretty basic setup here that uses a white-list approach to building up a list. We want to write some tests to mock this behavior. If we wanted two different return values when this method is called, we might instinctively write a test like this:<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code12'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13412"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p134code12"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> LastSetupWins<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
var mockReturnStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
var usesStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UsesReturnedStuff<span style="color: #008000;">&#40;</span>mockReturnStuff<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
mockReturnStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Hello&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
mockReturnStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;World!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var stuff <span style="color: #008000;">=</span> usesStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStuff</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</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: #666666;">&quot;Hello World!&quot;</span>, <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, stuff<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
If you couldn&#8217;t already tell from the name of the test method, this won&#8217;t work as the last return value to be Setup wins. The output from this test will produce the text:</p>
<blockquote><p>&#8220;World! World!&#8221;</p></blockquote>
<p>Not so great, but the Moq documentation gives us a workaround by using a callback method. A little bit of refactoring and Voila!<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code13'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13413"><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" id="p134code13"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetupWithCallback<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var mockReturnStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var usesStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UsesReturnedStuff<span style="color: #008000;">&#40;</span>mockReturnStuff<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var returnValues <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;Hello&quot;</span>, <span style="color: #666666;">&quot;World!&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
    var numCalls <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
    mockReturnStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> returnValues<span style="color: #008000;">&#91;</span>numCalls<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Callback</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> numCalls<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var stuff <span style="color: #008000;">=</span> usesStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStuff</span><span style="color: #008000;">&#40;</span>returnValues<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</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: #666666;">&quot;Hello World!&quot;</span>, <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, stuff<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
Here we are taking advantage of anonymous methods and closures to essentially keep feeding our Mock service it&#8217;s next value. This works, but&#8230; yuck! Reading this test is not very clear, and it gets in the way of the intent.<br />
Since we are supposed to refactor our test code too, lets move the ugly bits out into a private method, and re-write our test again.<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code14'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13414"><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
</pre></td><td class="code" id="p134code14"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> Int32 SetupMany<span style="color: #008000;">&#40;</span>Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span> mock, Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>IReturnStuff, <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">&gt;&gt;</span> expression, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var numCalls <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
    mock<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> args<span style="color: #008000;">&#91;</span>numCalls<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">.</span><span style="color: #0000FF;">Callback</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> numCalls<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> args<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetupWithExtractedMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var mockReturnStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var usesStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UsesReturnedStuff<span style="color: #008000;">&#40;</span>mockReturnStuff<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var calls <span style="color: #008000;">=</span> SetupMany<span style="color: #008000;">&#40;</span>mockReturnStuff,
                          svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
                          <span style="color: #666666;">&quot;Hello&quot;</span>, <span style="color: #666666;">&quot;World!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var stuff <span style="color: #008000;">=</span> usesStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStuff</span><span style="color: #008000;">&#40;</span>calls<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: #666666;">&quot;Hello World!&quot;</span>, <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, stuff<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
This is good. We haven&#8217;t lost any functionality, but the intent of the test is much clearer. There is less friction because you can read the code and understand what it is doing pretty quickly without having to &#8220;decipher&#8221; the meaning.<br />
Now, this is all fine and good, but I am likely to run into this same scenario more than once. What I would really love to have is a SetupMany() method that was built into Moq. A method I could call directly on the mocked object. Well&#8230; starting with C# 3.0 Extension Methods give us the ability to do just that. With a little work, we should be able to create a very generic extension to give us the desired behavior.<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code15'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13415"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p134code15"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> MoqExtensions
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetupMany<span style="color: #008000;">&lt;</span>TSvc, TReturn<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> Mock<span style="color: #008000;">&lt;</span>TSvc<span style="color: #008000;">&gt;</span> mock,
        Expression<span style="color: #008000;">&lt;</span>Func<span style="color: #008000;">&lt;</span>TSvc, TReturn<span style="color: #008000;">&gt;&gt;</span> expression,
        <span style="color: #0600FF; font-weight: bold;">params</span> TReturn<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #0600FF; font-weight: bold;">where</span> TSvc <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">class</span>
    <span style="color: #008000;">&#123;</span>
        var numCalls <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        mock<span style="color: #008000;">.</span><span style="color: #0000FF;">Setup</span><span style="color: #008000;">&#40;</span>expression<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">.</span><span style="color: #0000FF;">Returns</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> numCalls <span style="color: #008000;">&lt;</span> args<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">?</span> args<span style="color: #008000;">&#91;</span>numCalls<span style="color: #008000;">&#93;</span> <span style="color: #008000;">:</span> args<span style="color: #008000;">&#91;</span>args<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">.</span><span style="color: #0000FF;">Callback</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> numCalls<span style="color: #008000;">++</span><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>
Ok, so I know the method signature looks pretty scary. Just don&#8217;t stare at it long enough or you will go blind! The extra bit with the Ternary operator is a bit of error checking to prevent you from running over the bounds of the array. If you call the method more times than available arguments, it will simply keep returning the last argument in the list.<br />
Refactoring our test one last time gives us an even cleaner syntax that works with any return type. The second test merely demonstrates the &#8220;sticky&#8221; final value being returned over and over.<br />
</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p134code16'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13416"><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
</pre></td><td class="code" id="p134code16"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetupWithGenericExtensionMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var mockReturnStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var usesStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UsesReturnedStuff<span style="color: #008000;">&#40;</span>mockReturnStuff<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    mockReturnStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">SetupMany</span><span style="color: #008000;">&#40;</span>svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;Hello&quot;</span>, <span style="color: #666666;">&quot;World!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var stuff <span style="color: #008000;">=</span> usesStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStuff</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</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: #666666;">&quot;Hello World!&quot;</span>, <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, stuff<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&#91;</span>Test<span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> SetupWithLotsOfParams<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    var mockReturnStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mock<span style="color: #008000;">&lt;</span>IReturnStuff<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var usesStuff <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UsesReturnedStuff<span style="color: #008000;">&#40;</span>mockReturnStuff<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    mockReturnStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">SetupMany</span><span style="color: #008000;">&#40;</span>svc <span style="color: #008000;">=&gt;</span> svc<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnAString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>,
                              <span style="color: #666666;">&quot;the&quot;</span>,<span style="color: #666666;">&quot;quick&quot;</span>,<span style="color: #666666;">&quot;brown&quot;</span>,<span style="color: #666666;">&quot;fox&quot;</span>,<span style="color: #666666;">&quot;jumps&quot;</span>,<span style="color: #666666;">&quot;over&quot;</span>,<span style="color: #666666;">&quot;the&quot;</span>,<span style="color: #666666;">&quot;fence&quot;</span>,<span style="color: #666666;">&quot;hello!&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var stuff <span style="color: #008000;">=</span> usesStuff<span style="color: #008000;">.</span><span style="color: #0000FF;">GetStuff</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">11</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: #666666;">&quot;the quick brown fox the hello! hello! hello!&quot;</span>, <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, stuff<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>
And there you have it. Extending the Moq API without having to actually go in and modify the source. Hopefully this will save you a headache or two in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cyragon.com/2010/06/extending-moqs-setup-to-return-multiple-values/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
