<?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>Calle Arnesten&#039;s Blog &#187; Rhino Mocks</title>
	<atom:link href="http://www.arnesten.com/category/rhino-mocks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arnesten.com</link>
	<description>Web Development, Test Driven Development, NHibernate, ASP.NET MVC, jQuery and more..</description>
	<lastBuildDate>Sun, 10 Jan 2010 18:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>AutoStub parameters using Rhino Mocks</title>
		<link>http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/</link>
		<comments>http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 19:46:49 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Rhino Mocks]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/</guid>
		<description><![CDATA[For the last years I’ve been using dependency injection for my service classes. More recently (by the introduction of ASP.NET MVC) that have also been the case for my controller classes. I take in their dependencies via the constructor and then let a Dependency Injection/Inversion of Control (DI/IoC) framework automatically resolve and create the classes. [...]]]></description>
			<content:encoded><![CDATA[<p>For the last years I’ve been using dependency injection for my service classes. More recently (by the introduction of ASP.NET MVC) that have also been the case for my controller classes. I take in their dependencies via the constructor and then let a Dependency Injection/Inversion of Control (DI/IoC) framework automatically resolve and create the classes. I’ve been using Castle Windsor for this, which has worked really well.</p>
<p>However, when it comes to testing service/controller classes, I’ve previously created (often stubbed) the parameters needed when testing, and then given them manually to the constructor. Like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> customerDao = Stub&lt;<span style="color: #2b91af;">OrderDao</span>&gt;();
<p style="margin: 0px;"><span style="color: blue;">var</span> orderDao = Stub&lt;<span style="color: #2b91af;">OrderDao</span>&gt;();
<p style="margin: 0px;"><span style="color: blue;">var</span> controller = <span style="color: blue;">new</span> <span style="color: #2b91af;">CustomerController</span>(customerDao, orderDao);
</div>
</pre>
<p>This has proven to be quite fragile as the dependencies to the service/controller class has changed. Often, the case has been that more dependencies have been added as parameters to the constructor. I know, I know, that might be a code smell that the service/controller should be split into smaller classes… But anyway, code smell or not, the fragility of the tests is a problem.</p>
<p>A while back I read about an AutoMocking container, but thought it was a bit cumbersome to have to use both a Mocking framework and a DI/IoC container in my tests. So, I thought that this should be possibly to accomplish with only a mocking framework and a bit of reflection magic. And it actually turned out to be really easy to do.</p>
<p>As a mocking framework I use Rhino Mocks (btw, I love the new AAA syntax in 3.5!). All my test inherit from a class called BaseTest, which is where I decided to put the autostubbing. The code comes here:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;">[<span style="color: #2b91af;">TestFixture</span>]
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T Stub&lt;T&gt;(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters) <span style="color: blue;">where</span> T : <span style="color: blue;">class</span>
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateStub&lt;T&gt;(parameters);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T Mock&lt;T&gt;(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters) <span style="color: blue;">where</span> T : <span style="color: blue;">class</span>
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateMock&lt;T&gt;(parameters);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T CreateAndAutoStubParameters&lt;T&gt;(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> (T)CreateAndAutoStubParameters(<span style="color: blue;">typeof</span>(T), parameters);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> CreateAndAutoStubParameters(<span style="color: #2b91af;">Type</span> type, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructor = type.GetConstructors(
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.Instance)[0];
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> parameterInfos = constructor.GetParameters();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructorParams = GetConstructorParameters(
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameterInfos, parameters);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> constructor.Invoke(constructorParams.ToArray());
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">object</span>&gt; GetConstructorParameters(<span style="color: #2b91af;">ParameterInfo</span>[] parameterInfos, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">params</span> <span style="color: blue;">object</span>[] parameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructorParams = <span style="color: blue;">new</span> <span style="color: #2b91af;">List</span>&lt;<span style="color: blue;">object</span>&gt;();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> info <span style="color: blue;">in</span> parameterInfos)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; constructorParams.Add(GetParameter(info, parameters));
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> constructorParams;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> GetParameter(<span style="color: #2b91af;">ParameterInfo</span> info, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">object</span>[] candidateParameters)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">foreach</span> (<span style="color: blue;">var</span> candidate <span style="color: blue;">in</span> candidateParameters)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (info.ParameterType.IsAssignableFrom(candidate.GetType()))
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> candidate;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> AutoStub(info.ParameterType);
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> T AutoStub&lt;T&gt;() <span style="color: blue;">where</span> T : <span style="color: blue;">class</span>
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> (T)AutoStub(<span style="color: blue;">typeof</span>(T));
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">protected</span> <span style="color: blue;">static</span> <span style="color: blue;">object</span> AutoStub(<span style="color: #2b91af;">Type</span> type)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (type.IsInterface)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateStub(type);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructor = type.GetConstructors(
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">BindingFlags</span>.Public | <span style="color: #2b91af;">BindingFlags</span>.Instance)[0];
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> parameterInfos = constructor.GetParameters();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> constructorParams = GetConstructorParameters(parameterInfos);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> <span style="color: #2b91af;">MockRepository</span>.GenerateStub(type, constructorParams.ToArray());
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>So, now let us assume you want to test a CustomerController with dependencies like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CustomerController</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> CustomerController(<span style="color: #2b91af;">CustomerDao</span> customerDao, <span style="color: #2b91af;">OrderDao</span> orderDao)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: green;">// Code here..</span>
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// Code here..</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>Then you only have to write this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> controller = CreateAndAutoStubParameters&lt;<span style="color: #2b91af;">CustomerController</span>&gt;();
</div>
</pre>
<p>to create the controller with stubs automatically created as constructor parameters.<br />
Often, you might want to have some control over one of the parameters, and then you could write like this:</p>
<pre class="csharpcode">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: blue;">var</span> customerDao = AutoStub&lt;<span style="color: #2b91af;">CustomerDao</span>&gt;();
<p style="margin: 0px;">customerDao.Stub(c =&gt; c.GetById(1)).Return(<span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span>());
<p style="margin: 0px;"><span style="color: blue;">var</span> controller = CreateAndAutoStubParameters&lt;<span style="color: #2b91af;">CustomerController</span>&gt;(customerDao);
</div>
</pre>
<p>This way of testing service and controller classes has removed some of the pain of starting to test classes with dependencies that need to be mocked or stubbed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/16/autostub-parameters-using-rhino-mocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

