<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>igorbrejc.net &#187; mocks</title>
	<atom:link href="http://igorbrejc.net/category/development/testing/mocks/feed" rel="self" type="application/rss+xml" />
	<link>http://igorbrejc.net</link>
	<description>Just another developer's weblog</description>
	<lastBuildDate>Sun, 12 Feb 2012 06:47:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Rhino.Mocks Do() handler using anonymous delegates</title>
		<link>http://igorbrejc.net/development/c/rhinomocks-do-handler-using-anonymous-delegates</link>
		<comments>http://igorbrejc.net/development/c/rhinomocks-do-handler-using-anonymous-delegates#comments</comments>
		<pubDate>Tue, 05 Feb 2008 07:09:03 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/development/c/rhinomocks-do-handler-using-anonymous-delegates</guid>
		<description><![CDATA[A reminder post for me on how to use the Rhino.Mocks Do() handler together with anonymous delegates. This is useful when you want to set a method call expectation and the method accepts some complex type as the parameter. Using assertions you can set expectations for the parameter&#8217;s value with your own custom logic. There [...]]]></description>
			<content:encoded><![CDATA[<p>A reminder post for me on how to use the <a href="http://www.ayende.com/Wiki/(S(hkd1uw55rhtsalfn54sj3455))/Default.aspx?Page=Rhino+Mocks+The+Do()+Handler">Rhino.Mocks Do() handler</a> together with anonymous delegates. This is useful when you want to set a method call expectation and the method accepts some complex type as the parameter. Using assertions you can set expectations for  the parameter&#8217;s value with your own custom logic. There are other ways of achieving this, off course, but this way is elegant for simple (few lines of code) cases.</p>

<p>The example code starts with defining an IStringProcessor interface (which will be mocked):</p>

<div class="dean_ch" style="white-space: wrap;"><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw4">interface</span> IStringProcessor<br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">void</span> ProcessString <span class="br0">&#40;</span><span class="kw4">string</span> value<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></div>

<p>This is an example interface, just to demonstrate the power of Do() handler.</p>

<p>In the test method we want to make sure that all calls to the IStringProcessor.ProcessString() method provide strings which contain the &#8216;text&#8217; substring. We then do the test by calling this method with three different string parameters:</p>

<div class="dean_ch" style="white-space: wrap;"><br />
&nbsp; &nbsp; <span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">void</span> RhinoDoExample <span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IStringProcessor mockProcessor = mocks.<span class="me1">CreateMock</span> &lt;IStringProcessor&gt; <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; mockProcessor.<span class="me1">ProcessString</span> <span class="br0">&#40;</span><span class="kw1">null</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; LastCall.<span class="me1">IgnoreArguments</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="kw1">Do</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>Action&lt;string&gt;<span class="br0">&#41;</span><span class="kw4">delegate</span> <span class="br0">&#40;</span><span class="kw4">string</span> value<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Assert.<span class="me1">IsTrue</span> <span class="br0">&#40;</span>value.<span class="me1">Contains</span> <span class="br0">&#40;</span><span class="st0">&quot;text&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">Repeat</span>.<span class="me1">Any</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; mocks.<span class="me1">ReplayAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; mockProcessor.<span class="me1">ProcessString</span> <span class="br0">&#40;</span><span class="st0">&quot;This is a dummy text.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; mockProcessor.<span class="me1">ProcessString</span> <span class="br0">&#40;</span><span class="st0">&quot;This is a dummy text2.&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; mockProcessor.<span class="me1">ProcessString</span> <span class="br0">&#40;</span><span class="st0">&quot;This is a dummy.&quot;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; mocks.<span class="me1">VerifyAll</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></div>

<p>The test method will fail because of the third call: &#8220;This is a dummy.&#8221; string does not contain the required &#8216;text&#8217; substring.</p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/c/rhinomocks-do-handler-using-anonymous-delegates/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
	</item>
	</channel>
</rss>

