<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://footheory.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>this.Reflect(); - Donn&amp;#39;s Weblog : TDD</title><link>http://footheory.com/blogs/donnfelker/archive/tags/TDD/default.aspx</link><description>Tags: TDD</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP2 (Debug Build: 20611.960)</generator><item><title>Intro to Microsoft Unity - Dependency Injection Container</title><link>http://footheory.com/blogs/donnfelker/archive/2008/05/25/intro-to-microsoft-unity-dependency-injection-container.aspx</link><pubDate>Mon, 26 May 2008 03:40:42 GMT</pubDate><guid isPermaLink="false">9ce7e6ef-4587-4f0e-939d-3f75f3a8ddfc:385</guid><dc:creator>donnfelker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://footheory.com/blogs/donnfelker/rsscomments.aspx?PostID=385</wfw:commentRss><comments>http://footheory.com/blogs/donnfelker/archive/2008/05/25/intro-to-microsoft-unity-dependency-injection-container.aspx#comments</comments><description>&lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Note: A full solution is supplied at the bottom of this post if you would like to download it and run it. &lt;/p&gt;  &lt;p&gt;A couple of days ago &lt;a href="http://footheory.com/blogs/matt" target="_blank"&gt;Matt&lt;/a&gt; wrote about the new &lt;a href="http://codeplex.com/unity" target="_blank"&gt;Unity Application Block&lt;/a&gt;. The &lt;a href="http://msdn.microsoft.com/en-us/library/cc468366.aspx" target="_blank"&gt;Unity MSDN article&lt;/a&gt; describes Unity as: &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;The Unity Application Block (Unity) is a lightweight, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages: &lt;/p&gt;    &lt;ul&gt;     &lt;li&gt;Simplified object creation, especially for hierarchical object structures and dependencies &lt;/li&gt;      &lt;li&gt;Abstraction of requirements; this allows developers to specify dependencies at run time or in configuration and simplify management of crosscutting concerns &lt;/li&gt;      &lt;li&gt;Increased flexibility by deferring component configuration to the container &lt;/li&gt;      &lt;li&gt;Service location capability; this allows clients to store or cache the container &lt;/li&gt;   &lt;/ul&gt; &lt;/blockquote&gt;  &lt;p&gt;Designing a software system architecture with loosely coupled components makes life easier in the long run. A few of the benefits of loose coupling are : maximizing testability, modularizing components, and increase the maintainability of the code. &lt;/p&gt;  &lt;p&gt;This is a simple introduction into Unity that exemplifies the simplicity of the container setup. I will cover two options for type registration:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt; Manual Method based type Registration&lt;/li&gt;    &lt;li&gt;Configuration file (XML Based) type Registration&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;The Example Application&lt;/h3&gt;  &lt;p&gt;The application that is included at the bottom of this post is visualized via the class diagram below. This simple application utilizes very simple implementations of the &lt;a href="http://martinfowler.com/eaaCatalog/repository.html" target="_blank"&gt;repository&lt;/a&gt;, &lt;a href="http://martinfowler.com/eaaCatalog/dataMapper.html" target="_blank"&gt;mapper&lt;/a&gt;, &lt;a href="http://martinfowler.com/eaaCatalog/domainModel.html" target="_blank"&gt;domain model&lt;/a&gt;, &lt;a href="http://www.martinfowler.com/eaaCatalog/dataTransferObject.html" target="_blank"&gt;data transfer object&lt;/a&gt; (DTO) and &lt;a href="http://martinfowler.com/eaaCatalog/serviceLayer.html" target="_blank"&gt;service layer&lt;/a&gt; patterns for example purposes. &lt;/p&gt;  &lt;p&gt;The application follows a very simple flow as shown below: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 0px 0px;border-left:0px;border-bottom:0px;" height="287" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_thumb_1.png" width="590" align="left" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;The console application calls into the Service layer which calls into the repository to get the data which calls into the DB. A domain object is created from the Repository call, which is passed up to the service layer. The Service layer returns a DTO via the DTO Mapper to the parent caller (the console app) and then the console app displays the customer name via a write line call. &lt;/p&gt;  &lt;p&gt;None of these layers have any inter-dependent dependencies coded inside of them. I&amp;#39;m using constructor injection to inject dependencies into the calling object. &lt;/p&gt;  &lt;p&gt;Once unity is set up, it will find the dependencies and supply them to the program at runtime. Therefore, if I need a ICustomerRepository for the service layer to work properly, Unity will give the Service layer a CustomerRepository instance. Since CustomerRepository inherits from ICustomerRepository Unity is able to make this link (once you&amp;#39;ve set it up) and supply the proper instance. &lt;/p&gt;  &lt;h3&gt;Class Diagram&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_2.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="512" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_thumb.png" width="747" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;The Setup &amp;amp; Code&lt;/h3&gt;  &lt;p&gt;The easiest way to see HOW Unity works is to get into the code. My recommendation is to download the sample below, fire it up in Visual Studio and set some break points to see how the code is interacting. Seeing is believing! &lt;/p&gt;  &lt;p&gt;Before Unity can supply your application types at runtime, you have to tell Unity WHAT you want it to supply and where to find it. This can be done multiple ways as &lt;a href="http://msdn.microsoft.com/en-us/library/cc440947.aspx" target="_blank"&gt;described here&lt;/a&gt;. Today we will go over manual configuration inside of the console app, and xml configuration. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Manual Configuration&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 0px 0px;border-left:0px;border-bottom:0px;" height="182" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_7.png" width="582" align="left" border="0" /&gt; &lt;/p&gt;  &lt;p&gt;The manual configuration that I&amp;#39;ve utilized here utilizes the fluent interface of the container. &lt;/p&gt;  &lt;p&gt;Explanation of code: Here we are telling Unity to register a type. We&amp;#39;re saying &amp;quot;If someone requests ICustomerRepository, give them an instance of CustomerRepository. If someone requests ICustomerDTOMapper, then give them an instance of CustomerDTOMapper. ...&amp;quot; and so on. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Using the Container From Manual Configuration&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;After you&amp;#39;ve set up the types of objects the container should resolve, you can then ask the container for a instance of &amp;quot;ICustomerService&amp;quot; and then invoke a method call on it. At this very point in time Unity will give you the CustomerService, and then when the customer service needs a repository Unity will then give it a instance of CustomerRepository, etc. At this point in time, everything is mapped for you behind the scenes. The only thing you have to do is ask for the first object and Unity will walk the dependency graph and fill in the blanks for you (if as long as you&amp;#39;ve given Unity the proper types it needs). &lt;/p&gt;  &lt;p&gt;Here&amp;#39;s how to do this in the code: &lt;/p&gt;  &lt;p&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="97" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_10.png" width="642" align="left" border="0" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;XML Configuration&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;XML configuration utilizes an external XML file (app.config/web.config) to create its dependency graph. This allows for a lot more freedom in what you would like to do with your app. If you&amp;#39;re not happy with a certain version of &amp;quot;ICustomerRepository&amp;quot; you can create a new one, drop the DLL in the bin folder and change this config file to point to the new one and you&amp;#39;re done. No recompiling! &lt;/p&gt;  &lt;p&gt;In this implementation I&amp;#39;m using the app.config file because this is a Console App for example purposes. The full schema for XML configuration can be found &lt;a href="http://msdn.microsoft.com/en-us/library/cc440942.aspx" target="_blank"&gt;here on the MSDN&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;To utilize the XML configuration you&amp;#39;ll need to utilize a custom config section. You can do this by placing this code into your config section of your app.config file: &lt;/p&gt;  &lt;p&gt;&lt;a href="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_12.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="129" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_thumb_4.png" width="765" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Next, you&amp;#39;ll need to add the container and type definition as shown below (this is just a snippet, full code is included at the bottom of the post): &lt;/p&gt;  &lt;p&gt;&lt;a href="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_14.png"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 10px 0px 0px;border-left:0px;border-bottom:0px;" height="337" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_thumb_5.png" width="887" align="left" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The container gets its types from the config file at runtime. &lt;/p&gt;  &lt;p&gt;Each type is declared in the config file as the &amp;quot;type&amp;quot; of object to find and the &amp;quot;mapTo&amp;quot; defines which object to return when the &amp;quot;type&amp;quot; is requested.&lt;/p&gt;  &lt;p&gt;The type definitions are declared as type, assembly. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Using the Container From XML Configuration&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;To force Unity to use the XML Configuration you have to perform few extra set up steps in the application code as shown below. Essentially you&amp;#39;re telling Unity that it&amp;#39;s configuration/mappings are stored in a config file. The config section configures the container for you, as shown below. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_16.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="69" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_thumb_6.png" width="954" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;To have Unity resolve an instance, its the same as the manual configuration option above. Here is the same image from above that displays how to do this: &lt;/p&gt;  &lt;p&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="97" alt="image" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/IntrotoMicrosoftUnityDependencyInjection_11046/image_10.png" width="642" align="left" border="0" /&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;Overall, using Unity as quite simple for this example. The docs on MSDN were pretty good, however I didn&amp;#39;t really like the examples that were contained with the &lt;a href="http://codeplex.com/unity" target="_blank"&gt;download&lt;/a&gt; because they did not show you how to utilize XML configuration as I wanted to. It didn&amp;#39;t take but a few minutes to figure out what I needed to do. I think Unity is a decent solution at this time. I plan to investigate further into the depths of Unity to see if it may possibly be an alternative for my favorite container tool, &lt;a href="http://www.castleproject.org/container/index.html" target="_blank"&gt;Windsor Container&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Download The Code For This Post&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://blog.donnfelker.com/content/binary/FooTheory.UnityExample.zip" target="_blank"&gt;FooTheory.UnityExample.zip (~477K)&lt;/a&gt;&lt;/p&gt;&lt;img src="http://footheory.com/aggbug.aspx?PostID=385" width="1" height="1"&gt;</description><category domain="http://footheory.com/blogs/donnfelker/archive/tags/TDD/default.aspx">TDD</category><category domain="http://footheory.com/blogs/donnfelker/archive/tags/Agile/default.aspx">Agile</category><category domain="http://footheory.com/blogs/donnfelker/archive/tags/.NET/default.aspx">.NET</category><category domain="http://footheory.com/blogs/donnfelker/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category></item><item><title>TIP: Utilizing Fluent Syntax with the "Is" Keyword in Rhino Mocks and NUnit</title><link>http://footheory.com/blogs/donnfelker/archive/2008/05/23/tip-utilizing-fluent-syntax-with-the-quot-is-quot-keyword-in-rhino-mocks-and-nunit.aspx</link><pubDate>Fri, 23 May 2008 18:13:58 GMT</pubDate><guid isPermaLink="false">9ce7e6ef-4587-4f0e-939d-3f75f3a8ddfc:384</guid><dc:creator>donnfelker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://footheory.com/blogs/donnfelker/rsscomments.aspx?PostID=384</wfw:commentRss><comments>http://footheory.com/blogs/donnfelker/archive/2008/05/23/tip-utilizing-fluent-syntax-with-the-quot-is-quot-keyword-in-rhino-mocks-and-nunit.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://www.ayende.com/Wiki/(S(wdhuxa45k2tuua45qbqia445))/Rhino+Mocks+Constraints.ashx" target="_blank"&gt;Rhino Mocks&lt;/a&gt; and &lt;a href="http://nunit.org" target="_blank"&gt;NUnit&lt;/a&gt; both have a &amp;quot;Is&amp;quot; keyword, both used for a fluent style of syntax. Unfortunately this causes testing problems when you need to use them together. The .NET compiler can only understand one &amp;quot;Is&amp;quot; keyword at a time. We can&amp;#39;t use both unless we full qualify the second one. &lt;/p&gt;  &lt;p&gt;Having said that, I&amp;#39;ve fully qualified the &amp;quot;Is&amp;quot; keyword for Rhino Mocks and NUnit before and I find it clunky and I feel its a code smell to me. &lt;/p&gt;  &lt;h5&gt;In the figure below an example of a fully qualified &amp;quot;Is&amp;quot; keyword for Rhino Mocks is declared. &lt;/h5&gt;  &lt;p&gt;   &lt;div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8704bd3e-170d-4e31-8c9b-bb81036745f1" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;&lt;pre style="background-color:White;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;[Test]
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ExampleTest()
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;{
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; ... items ommited for brevity&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#008000;"&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;    With.Mocks(mockery)
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;    .Expecting(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;    {
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;       Expect.Call(databaseMock.GetStoredProcCommand(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;))
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;           .IgnoreArguments()
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;           .Constraints(Rhino.Mocks.Constraints.Is.NotNull())
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;           .Return(databaseCommandStub); 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;13&lt;/span&gt; &lt;span style="color:#000000;"&gt;    })
&lt;/span&gt;&lt;span style="color:#008080;"&gt;14&lt;/span&gt; &lt;span style="color:#000000;"&gt;    .Verify(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;15&lt;/span&gt; &lt;span style="color:#000000;"&gt;    {
&lt;/span&gt;&lt;span style="color:#008080;"&gt;16&lt;/span&gt; &lt;span style="color:#000000;"&gt;        IQuery query &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ExampleResultQuery.GetInsertResultQueryFrom(someparameter);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;17&lt;/span&gt; &lt;span style="color:#000000;"&gt;        IDatabaseCommand command &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; query.PrepareCommandUsing(databaseMock);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;18&lt;/span&gt; &lt;span style="color:#000000;"&gt;        Assert.That(command, Is.SameAs(databaseCommandStub)); 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;19&lt;/span&gt; &lt;span style="color:#000000;"&gt;    }); 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;20&lt;/span&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;On line 11, we can see the fully qualified name: Rhino.Mocks.Constraints.Is.NotNull() method called. &lt;/p&gt;

&lt;p&gt;This, my friends, is a &lt;a href="http://en.wikipedia.org/wiki/Code_smell" target="_blank"&gt;code smell&lt;/a&gt; (&lt;a href="http://en.wikipedia.org/wiki/IMO" target="_blank"&gt;IMO&lt;/a&gt;). &lt;/p&gt;

&lt;h5&gt;&amp;#160;&lt;/h5&gt;

&lt;h3&gt;How To Fix&lt;/h3&gt;

&lt;p&gt;The NUnit testing framework provides a &amp;quot;Iz&amp;quot; syntax helper that VB.NET developers use because &amp;quot;Is&amp;quot; in VB.NET is a keyword. &lt;/p&gt;

&lt;p&gt;Since we&amp;#39;re using .NET Managed code we can use the Iz keyword and it will achieve the same results as Is did. Therefore we can use the Is keyword in Rhino Mocks and we can use the Iz keyword in NUnit. This saves a lot of key strokes over the course of a day. &lt;/p&gt;

&lt;p&gt;Final Example: &lt;/p&gt;

&lt;p&gt;
  &lt;div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ad01b45c-f5fc-4b80-8306-b9b73544fea3" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;&lt;pre style="background-color:White;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;[Test]
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;void&lt;/span&gt;&lt;span style="color:#000000;"&gt; ExampleTest()
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;{
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#008000;"&gt;//&lt;/span&gt;&lt;span style="color:#008000;"&gt; ... items ommited for brevity&lt;/span&gt;&lt;span style="color:#008000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#008000;"&gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;    
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;    With.Mocks(mockery)
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;    .Expecting(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;    {
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;       Expect.Call(databaseMock.GetStoredProcCommand(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;))
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;           .IgnoreArguments()
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;           .Constraints(Is.NotNull())
&lt;/span&gt;&lt;span style="color:#008080;"&gt;13&lt;/span&gt; &lt;span style="color:#000000;"&gt;           .Return(databaseCommandStub); 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;14&lt;/span&gt; &lt;span style="color:#000000;"&gt;    })
&lt;/span&gt;&lt;span style="color:#008080;"&gt;15&lt;/span&gt; &lt;span style="color:#000000;"&gt;    .Verify(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;delegate&lt;/span&gt;&lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;16&lt;/span&gt; &lt;span style="color:#000000;"&gt;    {
&lt;/span&gt;&lt;span style="color:#008080;"&gt;17&lt;/span&gt; &lt;span style="color:#000000;"&gt;        IQuery query &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; ExampleResultQuery.GetInsertResultQueryFrom(someparameter);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;18&lt;/span&gt; &lt;span style="color:#000000;"&gt;        IDatabaseCommand command &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; query.PrepareCommandUsing(databaseMock);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;19&lt;/span&gt; &lt;span style="color:#000000;"&gt;        Assert.That(command, Iz.SameAs(databaseCommandStub)); 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;20&lt;/span&gt; &lt;span style="color:#000000;"&gt;    }); 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;21&lt;/span&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;Please note: Line 12 has the normal &amp;quot;Is.NotNull()&amp;quot; call from Rhino Mocks, and Line 19 utilizes the Iz syntax helper from NUnit. &lt;/p&gt;

&lt;p&gt;I find this much easier on the wrist. :) &lt;/p&gt;&lt;img src="http://footheory.com/aggbug.aspx?PostID=384" width="1" height="1"&gt;</description><category domain="http://footheory.com/blogs/donnfelker/archive/tags/TDD/default.aspx">TDD</category><category domain="http://footheory.com/blogs/donnfelker/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Beginner Test Driven Development Habits</title><link>http://footheory.com/blogs/donnfelker/archive/2008/03/23/beginner-test-driven-development-habits.aspx</link><pubDate>Sun, 23 Mar 2008 18:28:05 GMT</pubDate><guid isPermaLink="false">9ce7e6ef-4587-4f0e-939d-3f75f3a8ddfc:358</guid><dc:creator>donnfelker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://footheory.com/blogs/donnfelker/rsscomments.aspx?PostID=358</wfw:commentRss><comments>http://footheory.com/blogs/donnfelker/archive/2008/03/23/beginner-test-driven-development-habits.aspx#comments</comments><description>&lt;p&gt;Having worked on quite a few teams where I&amp;#39;ve been the lead responsible for helping implement TDD or been an Agile Coach - I&amp;#39;ve seen a group of habits develop when teams first start utilizing &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank"&gt;TDD&lt;/a&gt;. These are ... &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt; 1. No test isolation&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;All functionality that is being tested is in one test. Tests are heavily over specified. &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;How to fix: Spend some time &lt;a href="http://en.wikipedia.org/wiki/Pair_programming" target="_blank"&gt;pairing&lt;/a&gt; with the developers that are having the issue. Help explain why we want to break tests apart into smaller units, aka: &amp;quot;UNIT&amp;quot; tests. Let them know that we do not want to test the entire class in one unit test. Just because it can be done doesn&amp;#39;t mean it should be done. Ya know, I COULD drive into oncoming traffic, but its just not a good idea. :) &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;2. Failure to recognize the TDD Mantra&lt;/strong&gt;&amp;#160;&lt;strong&gt;&lt;a href="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/BeginnerTDDHabits_64D9/tddcycle_2.jpg"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="630" alt="tddcycle" src="http://footheory.com/blogs/donnfelker/WindowsLiveWriter/BeginnerTDDHabits_64D9/tddcycle_thumb.jpg" width="517" align="right" border="0" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Most&amp;#160; new TDD&amp;#39;rs will go from Red to Green to - I&amp;#39;m done! &lt;/em&gt;&lt;em&gt;Nope. I&amp;#39;m sorry guys, we have one more in that list - Refactor. Let&amp;#39;s say it together.&amp;#160; Red/Green/&lt;u&gt;ReFactor&lt;/u&gt;, Red/Green/&lt;u&gt;ReFactor&lt;/u&gt; :) &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;How To Fix: Help the developers realize that we want to refactor for a reason. We have a passing test, we know what to expect from our results. Now lets clean up our code, maybe introduce some interfaces, change some variable names, introduce some fields, extract some methods, etc. Doing this allows us to clean up the code as well as have the confidence that our changes did not muck up the expected result. Run the unit tests after you refactor. If it breaks, oops, lets go figure out WHY it broke. We caught the error early, its a lot cheaper now. :) &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;3. The previous lends itself to Insufficient Test Coverage/TDD is my new Debugger&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;The developers usually will do enough to get the test passing and then move onto the next task. They think of TDD as a new debugger. &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;How To Fix: Sit down with the developers that have this issue and explain: Just because we can now unit test a method/object and step through it with a debugger doesn&amp;#39;t mean that this is the new debugger.&amp;#160; That&amp;#39;s not the point. Just because the test passes doesn&amp;#39;t mean we&amp;#39;re done. Just because your method passes if you pass a valid object into it doesn&amp;#39;t mean that its fully tested. What happens if I pass a null object into it? What happens if I pass a new&amp;#39;d up object with nothing instantiated? Have those instances been tested? Have we tested that our objects are interacting as we expect them to? Do we know if the Logger got called? How do we know if we didn&amp;#39;t test it?&amp;#160; That its the classic question: How do we know it works, if we don&amp;#39;t have a test to prove it? &lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;4. No concept of separation of concerns or &lt;/strong&gt;&lt;a href="http://martinfowler.com/articles/injection.html" target="_blank"&gt;&lt;strong&gt;IoC/dependency injection&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;. &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;This is not a big deal and I expect it of most new TDD converts. Usually while learning TDD the first few tests do not need &lt;/em&gt;&lt;a href="http://martinfowler.com/articles/injection.html" target="_blank"&gt;&lt;em&gt;DI/IoC&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, but this is something that will help in the future and will need to be understood to really grasp. This really comes into play when we start separating our concerns and breaking dependencies between objects. The refactoring that takes place here can really get deep into DI/IoC sometimes. &lt;/em&gt;&lt;/p&gt;  &lt;p&gt;How To Fix: Give them a great intro into interfaces and how they&amp;#39;re used. Show them great articles on DI/IoC written by &lt;a href="http://martinfowler.com/articles/injection.html" target="_blank"&gt;Martin Fowler&lt;/a&gt;, &lt;a href="http://codebetter.com/blogs/jeremy.miller/" target="_blank"&gt;Jeremy D. Miller&lt;/a&gt;, &lt;a href="http://www.ayende.com/Blog/" target="_blank"&gt;Ayende&lt;/a&gt;, and have them join certain mailing lists such as &lt;a href="http://tech.groups.yahoo.com/group/altdotnet/" target="_blank"&gt;altdotnet&lt;/a&gt; to join in/read the discussions. On that list alone, DI/IoC discussions occur on a daily basis.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;5. TDD Complaints&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;I&amp;#39;ve seen complaints such as: &lt;/em&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;ol&gt;     &lt;li&gt;&lt;em&gt;It takes too long to write the tests&lt;/em&gt;&lt;/li&gt;      &lt;li&gt;&lt;em&gt;I write more test code than real code. &lt;/em&gt;&lt;/li&gt;      &lt;li&gt;&lt;em&gt;The tests are harder to write than the real code. &lt;/em&gt;&lt;/li&gt;      &lt;li&gt;&lt;em&gt;How is this going to help me get my project done? I can just hit F5 and test it and I can tell if it works or not with my own eyes.&lt;/em&gt;&lt;/li&gt;      &lt;li&gt;&lt;em&gt;Who cares if it passes a unit test, I&amp;#39;m the only person who works on this project. &lt;/em&gt;&lt;/li&gt;   &lt;/ol&gt; &lt;/ol&gt;  &lt;p&gt;How To Fix: This is one of those habits I&amp;#39;ve seen in every place I&amp;#39;ve coached. These are classic examples of someone who hasn&amp;#39;t &amp;quot;seen the light&amp;quot; of TDD yet. In my experience, the easiest way to help them see the light is to &lt;a href="http://en.wikipedia.org/wiki/Pair_programming" target="_blank"&gt;pair&lt;/a&gt; with with them while writing unit tests. They will instantly see why the unit tests help them and the team. Every single time I do this I help the TDD newcomer realize that by writing this test they&amp;#39;ve exposed X bugs that they didn&amp;#39;t think about before. It usually goes like this: &amp;quot;Ok, that test passes, what would happen if an empty string was passed into this? Would the DB accept an empty string?&amp;quot; Them: &amp;quot;Oh... good point, I didn&amp;#39;t think of that.&amp;quot; Instantly lights start turning on. Another thing that works great to help developers see the light is to ensure that you&amp;#39;re utilizing &lt;a href="http://martinfowler.com/articles/continuousIntegration.html" target="_blank"&gt;Continuous Integration&lt;/a&gt; such as &lt;a href="http://ccnet.thoughtworks.com/" target="_blank"&gt;CCNET&lt;/a&gt;, or &lt;a href="http://www.jetbrains.com/teamcity/" target="_blank"&gt;TeamCity&lt;/a&gt;. Have them create a unit test for a section of code. Check it in. Watch it build and watch unit tests pass. . Then have them change the code (the code which is under test) and ensure it returns a different result. Maybe it returned a user&amp;#39;s name, such as &amp;quot;bob&amp;quot;, now have it return the users name with some characters appended to the end of it, such as &amp;quot;bob123&amp;quot;. Have them check it in, watch the build fail because the unit test failed. Explaining to them that this helps catch errors before the product is live is crucial. Once they have the &amp;quot;Ohhh!! I get it now moment&amp;quot;, they won&amp;#39;t want to stop writing tests. &lt;/p&gt;&lt;img src="http://footheory.com/aggbug.aspx?PostID=358" width="1" height="1"&gt;</description><category domain="http://footheory.com/blogs/donnfelker/archive/tags/TDD/default.aspx">TDD</category><category domain="http://footheory.com/blogs/donnfelker/archive/tags/Agile/default.aspx">Agile</category></item><item><title>Gleaning Clarity Through Unit Test Names</title><link>http://footheory.com/blogs/donnfelker/archive/2008/02/07/gleaning-clarity-through-unit-test-names.aspx</link><pubDate>Fri, 08 Feb 2008 04:56:19 GMT</pubDate><guid isPermaLink="false">9ce7e6ef-4587-4f0e-939d-3f75f3a8ddfc:342</guid><dc:creator>donnfelker</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://footheory.com/blogs/donnfelker/rsscomments.aspx?PostID=342</wfw:commentRss><comments>http://footheory.com/blogs/donnfelker/archive/2008/02/07/gleaning-clarity-through-unit-test-names.aspx#comments</comments><description>&lt;p&gt;You're unit testing, right? .... right? (If not... shame on you... ) It's known that &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank"&gt;TDD&lt;/a&gt;/nDD types of development dramatically increase the ability to refactor with confidence, increase code readability and help define and enforce business rules (in the tests). Unit testing helps with TONS of things in development (literally the list could go on and on), but what about the unit test names? What do those do for us as developers?&amp;#160; &lt;/p&gt;  &lt;h3&gt;Predecessors In The Name Game&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/rosherove/default.aspx"&gt;Roy Osherove&lt;/a&gt; has a &lt;a href="http://weblogs.asp.net/rosherove/archive/2005/04/03/TestNamingStandards.aspx" target="_blank"&gt;good post&lt;/a&gt; on Unit Test names. &lt;a href="http://www.jpboodhoo.com/blog" target="_blank"&gt;Jean-Paul&lt;/a&gt; also has tons of good info about unit test names on his blog. I tend to follow these same guidelines. Also - awhile back on the &lt;a href="http://tech.groups.yahoo.com/group/altdotnet/" target="_blank"&gt;altnet list&lt;/a&gt; there was a thread which spawned this post, but unfortunately I could not find it. If I do run across the post I'll augment this thread with the link. &lt;/p&gt;  &lt;h3&gt;Background&lt;/h3&gt;  &lt;p&gt;But what about when a bug report comes in? In my work I've used an alternative method so I thought I'd post it here. &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Lets say the bug report says that &amp;quot;CalculatePayment&amp;quot; is returning the wrong value when the system is in a certain state. This kind of test was completely missed during development. Heck, it happens! Your task is to implement a solution for this bug. &lt;/li&gt;    &lt;li&gt;Most likely you're going to write a unit test to assert that the solution you specified has been implemented as expected. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;h3&gt;Naming Convention&lt;/h3&gt;  &lt;p&gt;When naming these unit tests I will add the word FIX_&amp;lt;WorkItemNumber&amp;gt;_&amp;lt;MethodName&amp;gt;_&amp;lt;StateUnderTest&amp;gt;_&amp;lt;ExpectedBehavior&amp;gt;() as the method name. &lt;/p&gt;  &lt;p&gt;Example: &lt;/p&gt;  &lt;blockquote&gt;   &lt;pre class="csharpcode"&gt;FIX_186_CalculatePayment_WhenRateLessThan1000_ThrowPaymentException()
{ ... }&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;From this test fixture name I can glean that this is a fix for bug 186, for the CalculatePayment method, for the state when a rate is less than 1000, the method should throw an exception. I've now documented my code as well as created a very readable and understandable unit test name. &lt;/p&gt;

&lt;p&gt;I know this is not something everyone wants to do or would do, but in certain companies I've worked at this has helped with work item issue resolution and tracking. &lt;/p&gt;&lt;img src="http://footheory.com/aggbug.aspx?PostID=342" width="1" height="1"&gt;</description><category domain="http://footheory.com/blogs/donnfelker/archive/tags/TDD/default.aspx">TDD</category></item></channel></rss>