<?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; TDD</title>
	<atom:link href="http://www.arnesten.com/category/tdd/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>Setting non-public property with type safety (no more strings)</title>
		<link>http://www.arnesten.com/2008/11/30/setting-non-public-property-with-type-safety-no-more-strings/</link>
		<comments>http://www.arnesten.com/2008/11/30/setting-non-public-property-with-type-safety-no-more-strings/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 12:45:53 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=32</guid>
		<description><![CDATA[Sometimes I have a property where the get-part is public and the set-part is private or protected. Like this: public class Forum { &#160; &#160; public virtual int TopicCount { get; protected set; } &#160; &#160; &#160; // More code... } There can be various reasons for this design. For me it is often when [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes I have a property where the get-part is public and the set-part is private or protected. 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;">Forum</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">int</span> TopicCount { <span style="color: blue;">get</span>; <span style="color: blue;">protected</span> <span style="color: blue;">set</span>; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// More code...</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>There can be various reasons for this design. For me it is often when I make database optimizations where I don&#8217;t want to be able to change a property directly in the code, but instead do it directly against the database.</p>
<p>Still, sometimes I want to set the value in unit tests. How do I do that?</p>
<p>Before C# 3.0 I had to rely on having a string with the property name and then use reflection. The main disadvantage with this approach is that if you rename the property you have to make sure that the string is also changed, which is time-consuming and error-prone.</p>
<p>So, with C# 3.0 you no longer need that string. You could use the new Expression-tree feature like this instead:</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;">protected</span> <span style="color: blue;">static</span> <span style="color: blue;">void</span> SetProperty&lt;TObj, TValue&gt;(TObj obj, 
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: #2b91af;">Expression</span>&lt;<span style="color: #2b91af;">Func</span>&lt;TObj, TValue&gt;&gt; property, TValue value)
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">var</span> propertyInfo = (<span style="color: #2b91af;">PropertyInfo</span>)((<span style="color: #2b91af;">MemberExpression</span>)property.Body).Member;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; propertyInfo.SetValue(obj, value, <span style="color: blue;">null</span>);
<p style="margin: 0px;">}
</div>
</pre>
<p>I have placed this method in my BaseTest class, since I only use it in my tests. Now you can write code like this to set the property:</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> forum = <span style="color: blue;">new</span> <span style="color: #2b91af;">Forum</span>();
<p style="margin: 0px;">SetProperty(forum, f =&gt; f.TopicCount, 2);
</div>
</pre>
<p>This is only a scratch on the surface of what you can accomplish with Expression-trees.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/30/setting-non-public-property-with-type-safety-no-more-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing your NHibernate data access code using SQLite</title>
		<link>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/</link>
		<comments>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 21:28:54 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=30</guid>
		<description><![CDATA[Ever since I started working professionally at a software company (which is roughly 3 years ago) I’ve been doing unit testing. I really consider automatic testing one of the most (if not THE most) important trends of software development the last decade. But, despite that, I’ve let code close to the data access be left [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I started working professionally at a software company (which is roughly 3 years ago) I’ve been doing unit testing. I really consider automatic testing one of the most (if not THE most) important trends of software development the last decade. </p>
<p>But, despite that, I’ve let code close to the data access be left untested. This has been due to the pain I thought it would be to create and maintain those tests. And also I’ve thought that those tests would be way to slow to have among my other unit tests (which we want to be fast, so that we run them often).</p>
<p>For the first two years of my career this was not a big problem. We rarely had bugs in our data access code as it mostly consisted of simple, straight forward code. So I hadn’t really given it much thought. Until lately that is.</p>
<p>Then we started to get performance issues as we got some really large organizations to use or software. To resolve the performance issues we made our data access smarter so it could make more efficient queries to the database. As smarter often means more code, or at least added complexity, I saw the need of automatic tests to guarantee the quality of the code.</p>
<p>So, I read some articles on using SQLite with NHibernate. After some pain setting it all up, I accomplished my goal of easy data access testing. So say that I want to test GetCustomerByName in the following class (in real life I wouldn&#8217;t test methods that are this simple, but it&#8217;s just to show the idea):</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;">CustomerDao</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">readonly</span> <span style="color: #2b91af;">INHibernateManager</span> _sessionManager;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> CustomerDao(<span style="color: #2b91af;">INHibernateManager</span> sessionManager)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; _sessionManager = sessionManager;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">Customer</span> GetCustomerByName(<span style="color: blue;">string</span> name)
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> session = _sessionManager.CurrentSession;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> session.CreateQuery(<span style="color: #a31515;">&quot;from Customer where Name = :name&quot;</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .SetParameter(<span style="color: #a31515;">&quot;name&quot;</span>, name)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .UniqueResult&lt;<span style="color: #2b91af;">Customer</span>&gt;();
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>Then I can write a test 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;">CustomerDaoTest</span> : <span style="color: #2b91af;">BaseTest</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; [<span style="color: #2b91af;">Test</span>]
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> CanGetCustomerByName()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Database.Init();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerA = <span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span> { Name = <span style="color: #a31515;">&quot;A&quot;</span> };
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerB = <span style="color: blue;">new</span> <span style="color: #2b91af;">Customer</span> { Name = <span style="color: #a31515;">&quot;B&quot;</span> };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Database.Save(customerA, customerB);&nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> customerDao = <span style="color: blue;">new</span> <span style="color: #2b91af;">CustomerDao</span>(Database);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> result = customerDao.GetCustomerByName(<span style="color: #a31515;">&quot;B&quot;</span>);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Assert</span>.That(result, <span style="color: #2b91af;">Is</span>.EqualTo(customerB));
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>For this to work I have a class called TestDatabase with the following code:</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;">TestDatabase</span> : <span style="color: #2b91af;">INHibernateManager</span>
<p style="margin: 0px;">{
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">ISessionFactory</span> m_sessionFactory;&nbsp; &nbsp; &nbsp; &nbsp; 
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">SchemaExport</span> SchemaExport;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">private</span> <span style="color: #2b91af;">ISession</span> m_session;
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Init()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (m_sessionFactory == <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Configuration</span> config = CreateNHibernateConfig();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_sessionFactory = config.BuildSessionFactory();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SchemaExport = <span style="color: blue;">new</span> <span style="color: #2b91af;">SchemaExport</span>(config);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">if</span> (m_session != <span style="color: blue;">null</span>)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m_session.Close();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; m_session = m_sessionFactory.OpenSession();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; m_session.BeginTransaction();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">var</span> connection = m_session.Connection;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; SchemaExport.Execute(<span style="color: blue;">false</span>, <span style="color: blue;">true</span>, <span style="color: blue;">false</span>, <span style="color: blue;">false</span>, connection, <span style="color: blue;">null</span>);
<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;">Configuration</span> CreateNHibernateConfig()
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #2b91af;">Configuration</span> config = <span style="color: blue;">new</span> <span style="color: #2b91af;">Configuration</span>();
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.Properties.Clear();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.connection.driver_class&quot;</span>, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a31515;">&quot;NHibernate.Driver.SQLite20Driver&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.dialect&quot;</span>, <span style="color: #a31515;">&quot;NHibernate.Dialect.SQLiteDialect&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.connection.connection_string&quot;</span>, 
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #a31515;">&quot;Data Source=:memory:;Version=3;New=True;&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.cache.use_second_level_cache&quot;</span>, <span style="color: #a31515;">&quot;false&quot;</span>);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.SetProperty(<span style="color: #a31515;">&quot;hibernate.hbm2ddl.auto&quot;</span>, <span style="color: #a31515;">&quot;create&quot;</span>);
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; config.AddAssembly(<span style="color: #a31515;">&quot;MyAssembly&quot;</span>); <span style="color: green;">// Change this to your assembly</span>
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">return</span> config;
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ISession</span> Session
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_session; }
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Save(<span style="color: blue;">params</span> <span style="color: blue;">object</span>[] entities)
<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> entity <span style="color: blue;">in</span> entities)
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session.Save(entity);
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; Session.Flush();
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: #2b91af;">ISession</span> <span style="color: #2b91af;">INHibernateManager</span>.CurrentSession
<p style="margin: 0px;">&nbsp; &nbsp; {
<p style="margin: 0px;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: blue;">get</span> { <span style="color: blue;">return</span> m_session; }
<p style="margin: 0px;">&nbsp; &nbsp; }
<p style="margin: 0px;">}
</div>
</pre>
<p>And I’ve also added the following field to my BaseTest class:</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;">readonly</span> <span style="color: #2b91af;">TestDatabase</span> Database = <span style="color: blue;">new</span> <span style="color: #2b91af;">TestDatabase</span>();
<p style="margin: 0px;">&nbsp;
<p style="margin: 0px;">&nbsp; &nbsp; <span style="color: green;">// More code here...</span>
<p style="margin: 0px;">}
</div>
</pre>
<p>So, in summary, I&#8217;ve shown a pretty easy way of doing database testing. Even though the method under test was really simple in the example, the same technique could be used for much more complex scenarios.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/11/25/testing-your-nhibernate-data-access-code-using-sqlite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>
		<item>
		<title>JAOO 2007 – TDD tutorial with Robert C. Martin</title>
		<link>http://www.arnesten.com/2007/09/30/jaoo-2007-%e2%80%93-tdd-tutorial-with-robert-c-martin/</link>
		<comments>http://www.arnesten.com/2007/09/30/jaoo-2007-%e2%80%93-tdd-tutorial-with-robert-c-martin/#comments</comments>
		<pubDate>Sun, 30 Sep 2007 20:50:39 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2007/09/30/jaoo-2007-%e2%80%93-tdd-tutorial-with-robert-c-martin/</guid>
		<description><![CDATA[I arrived yesterday to Aarhus in Denmark for the ongoing JAOO conference. I’m here for two tutorials, one about Test-driven development (TDD) which I went to today, and one about domain-driven development (DDD) which I will visit tomorrow. The TDD tutuorial was held by Robert C. Martin, also called Uncle Bob, whom have written a [...]]]></description>
			<content:encoded><![CDATA[<p>I arrived yesterday to Aarhus in Denmark for the ongoing JAOO conference. I’m here for two tutorials, one about Test-driven development (TDD) which I went to today, and one about domain-driven development (DDD) which I will visit tomorrow.</p>
<p>The TDD tutuorial was held by <a href="http://www.objectmentor.com/omTeam/martin_r.html">Robert C. Martin</a>, also called Uncle Bob, whom have written a couple of books (none of which I have read yet, unfortunately). I found Robert to be charismatic, loud-mouthed (not in a negative way) and generally a funny guy. </p>
<p>The tutorial begun with a short introduction of TDD and then went on with a presentation of the requirements to a game called “Hunt the Wumpus” (wtf is a Wumpus?!). The requirements were written in a tool called <a href="http://www.fitnesse.org">FitNesse</a>. FitNesse is a popular tool for writing what is called acceptance tests. These acceptance tests should non-programmers, typically QA or management people, be able to write. </p>
<p>After the introduction we were given an incomplete implementation of the game, along with a lot of acceptance tests which didn’t pass. Our objective was to use TDD and write code to make the acceptance tests pass. Every change we made to the game code had to be as an effect of creating a unit test that failed.</p>
<p>I’ve personally been writing unit tests for two years now, but haven’t done any real TDD until very recently. So I’m still in the process of re-learning the way to write code. In TDD you should <b>“Write the test that force you write the code you want”</b> as Robert so elegantly put it. More formally there are three rules of TDD which are:</p>
<ol>
<li>You are not allowed to write any production code unless it is to make a failing unit test pass.</li>
<li>You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.</li>
<li>You are not allowed to write any more production code than is sufficient to pass the one failing unit test.</li>
</ol>
<p>If you practice TDD properly you should, accordingly to Robert, produce <b>more than 1000 unit tests per year</b>. That is a lot more than what I’ve produced my last year! And then I actually thought that I had been writing a lot of tests.. But when you think about it, it’s not really an unreasonable amount. Knock it out over a year and it’s just about 4 tests per day. Now it didn’t sound so much, did it?</p>
<p>Throughout the day, Robert interrupted regularly to ask the audience questions and share his opinions about difference aspects of software development. I will present some of the nuggets here:</p>
<p><b>“Comments are dirt in code”</b>. My favourite! What he means is that most of the time comments in code try to make up for poor design. If the design was really good, the comments wouldn’t be needed. I really agree with this reasoning when it regards that comments shouldn’t be needed inside code blocks. However, public comments that clarifies, for example what state a parameter must have (not null is common), I still think are needed sometimes, especially for APIs where you don’t have access to the source code.</p>
<p><b>“Implement new functionality in priority order, not what is convenient from a design point-of-view”</b>. This sentence stuck since we’ve been discussing this lately at the company I work for. The reasoning behind it (besides the obvious point that high priorities are more important than low priorites) is that the design should be refactored continually so that the best possibly design will emerge independently of the order that the tasks are implemented.</p>
<p>He also gave so guidelines of method design. <b>A well-designed method should:</b></p>
<ul>
<li>not be longer than 5 lines of code</li>
<li>not have indentation depth (like for and if statements) of more than 2</li>
<li>not have more than 2-3 parameters</li>
</ul>
<p>Personally, I&#8217;m not quite there yet when it comes to the first two statements. I try to make my methods short, but sometimes they are up to 30 lines of code.. And my indentation depth often goes up to three or sometimes even four.</p>
<p>At the end of the tutorial, there was time for reflections of working with TDD and a question from the audience was that they had gotten redundancies where some unit tests were almost identical as some of the acceptance tests. This was okay, according to Robert.</p>
<p>On the negative side I don&#8217;t have much to say.. Perhaps only that he at one time used exception handling for taking care of bad user input from the command line. In my book*, exception handling should only be used for exceptional events, not such a common case as when a user misspells a command..</p>
<p>All in all, I found the tutorial very giving and inspirational. How much the tutorial will affect my development process, I don&#8217;t know yet.. But I think I&#8217;ve gotten enough knowledge and energy to really start doing real test-driven development from now on. It will be fun to reflect upon this in a couple of weeks.</p>
<p>* No, I haven’t written any books. I mean the figure of speech.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/09/30/jaoo-2007-%e2%80%93-tdd-tutorial-with-robert-c-martin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

