<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Dennis Burton's Develop Using .NET - patterns</title>
    <link>http://developusing.net/</link>
    <description>Change is optional. Survival is not required.</description>
    <language>en-us</language>
    <copyright>Dennis Burton</copyright>
    <lastBuildDate>Tue, 19 May 2009 14:49:32 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>dennis@developusing.net</managingEditor>
    <webMaster>dennis@developusing.net</webMaster>
    <item>
      <trackback:ping>http://developusing.net/Trackback.aspx?guid=6a0a4830-21e5-4012-a67a-51adcc156475</trackback:ping>
      <pingback:server>http://developusing.net/pingback.aspx</pingback:server>
      <pingback:target>http://developusing.net/PermaLink,guid,6a0a4830-21e5-4012-a67a-51adcc156475.aspx</pingback:target>
      <dc:creator>Dennis Burton</dc:creator>
      <wfw:comment>http://developusing.net/CommentView,guid,6a0a4830-21e5-4012-a67a-51adcc156475.aspx</wfw:comment>
      <wfw:commentRss>http://developusing.net/SyndicationService.asmx/GetEntryCommentsRss?guid=6a0a4830-21e5-4012-a67a-51adcc156475</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <title>PatternsInTesting[3] - Spy Pattern</title>
      <guid isPermaLink="false">http://developusing.net/PermaLink,guid,6a0a4830-21e5-4012-a67a-51adcc156475.aspx</guid>
      <link>http://developusing.net/2009/05/19/PatternsInTesting3SpyPattern.aspx</link>
      <pubDate>Tue, 19 May 2009 14:49:32 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;Your mission, should you choose to accept it, is to observe the interaction with an object and verify that this interaction is in your best interest. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Scenario&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Some objects that you have to consume are just poorly written. The ones that are most egregious always seem to be the ones you have no control over. That lack of control may be because you have no access to the source, it may be because it would be a political minefield to change the source, or a lack of tests makes the team afraid to change the source. It seems like every time I go to a new client, these objects exist (as well as the political minefields). The developers have a mysterious set of incantations that they have memorized for interaction with these objects in order to avoid bugs. Often, no one knows where these &amp;quot;rules&amp;quot; came from and they are usually not written down. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Vocabulary&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The name of this pattern is the Spy. What the spy will do is capture information about interaction with an object, and only take action if the need arises. A spy object looks just like the object that you need to interact with (i.e. implements the same public interface) such that your code should not even notice it is even there, but, behind the scenes it will perform validation and give the useful feedback you wish the original object would have implemented in the first place. This spy or validation wrapper is commonly implemented by holding on to a reference to original object. This allows for the spy to call the actual implementation in order to preserve the original behavior. &lt;/p&gt;  &lt;blockquote&gt;   &lt;div&gt;     &lt;p&gt;&lt;strong&gt;The ideal world&lt;/strong&gt;&lt;/p&gt;      &lt;p&gt;In an ideal world, the kinds of interactions that you are trying to validate with a spy &lt;em&gt;should&lt;/em&gt; be captured be captured by the actual object you are interacting with, rather than the spy. Having a wrapper object whose function is validation is a massive code smell. If you have the ability to fix the original code by adding the relevant validation, that is by far a better solution than creating a spy object.&lt;/p&gt;      &lt;p&gt;&lt;strong&gt;The real world&lt;/strong&gt;&lt;/p&gt;      &lt;p&gt;In reality, you do not have access to change the source of third party libraries, even if sometimes that third party is a couple of buildings or even cubes away. The first thing you should do when you run in to these bizarre incantations “required” for successful object interaction, is to ask “why?” and be persistent, dig deep. You may be (not so) surprised that most of the reasons have long since &lt;a href="http://www.snopes.com/weddings/newlywed/secret.asp"&gt;gone away&lt;/a&gt;. If you do find that some of the hidden rules are indeed valid, you need a way to validate that your code is following the rules.&lt;/p&gt;   &lt;/div&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Consider the following example where &lt;em&gt;MethodToObserve&lt;/em&gt; will throw an uninformitive exception if the &lt;em&gt;PropertyToObserve&lt;/em&gt; has not yet been set. &lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IInterfaceToUse&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;void&lt;/span&gt; MethodToObserve();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; PropertyToObserve { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ClassToUse&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IInterfaceToUse&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; MethodToObserve()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PropertyToObserve.ForEach(str =&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Calling the MethodToObserve:&amp;quot;&lt;/span&gt; + str) );&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; propertyToObserve;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; PropertyToObserve&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 19&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine( &lt;span style="color: #a31515"&gt;&amp;quot;Calling the PropertyToObserve setter&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; propertyToObserve;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Calling the PropertyToObserve getter&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; propertyToObserve = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 27&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 28&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 29&lt;/span&gt; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;As mentioned previously, the ideal solution is to fix the implementation. If your only access to this code is Reflector or you are just not authorized to change it, the next best thing is to protect yourself (flaming email to the author of the code is,of course, optional). Our protection, or at least better information will come from a class implementing &lt;em&gt;IInterfaceToUse&lt;/em&gt; just like the original, only this time the implementation will provide the consumer with information that they can act on.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ValidatingObserver&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;IInterfaceToUse&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IInterfaceToUse&lt;/span&gt; _observedClass;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; ValidatingObserver(&lt;span style="color: #2b91af"&gt;IInterfaceToUse&lt;/span&gt; observedClass)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { _observedClass = observedClass; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; MethodToObserve()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (PropertyToObserve == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;PropertyToObserve&amp;quot;&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #a31515"&gt;&amp;quot;Property must be set prior to calling Method&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// perform observations&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;The spy is watching: MethodToObserve&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// pass through to implementing object&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _observedClass.MethodToObserve();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 19&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; PropertyToObserve&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;The spy is watching: PropertyToObserve getter&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; _observedClass.PropertyToObserve;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 27&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 28&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 29&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;The spy is watching: PropertyToObserve setter&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 30&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _observedClass.PropertyToObserve = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 31&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 32&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 33&lt;/span&gt; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Note that this time, instead of the “oops, I forgot something exception” known in .net as the “Object reference not set to instance of an object” exception, we get meaningful information about what is missing and even some hint as to how to fix it. The error now clearly states that the &lt;em&gt;PropertyToObserve&lt;/em&gt; should be set prior to calling the &lt;em&gt;MethodToObserve&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Using the spy&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;There are many ways to create a spy object, I chose containment for this post; You may also use derivation to create your wrapper. Derivation will get you up and running faster, and you will not have maintenance work to do if you add a method to the interface, but this will come at a cost. Containment will allow you to swap out the actual implementation of the object with a mock implementation at some time in the future. As always consider you needs before choosing a spy implementation.&lt;/p&gt;  &lt;p&gt;The test below shows how to use the spy created in this post&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt; [&lt;span style="color: #2b91af"&gt;TestFixture&lt;/span&gt;]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ManualObserverTests&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; MethodCallSpy()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; observedClass = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ClassToUse&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; validatingObserver = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ValidatingObserver&lt;/span&gt;(observedClass);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.Throws&amp;lt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;&amp;gt;( validatingObserver.MethodToObserve);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;Tool Support&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;All of the previous posts in this series have mentioned leveraging tools to assist in creating these test objects. They spy object however is a strange beast; the demands it places on the tools turn out to create as much code as the manually coded version. If you are so inclined, you can use Rhino for a spy object. What is required is taking advantage of the Do extension method. Do takes a delegate as a parameter that matches the signature of the method being called. So what you will end up establishing is an &lt;em&gt;Expect&lt;/em&gt;ation that a method will be called and when it is &lt;em&gt;Do&lt;/em&gt; the operation specified by the delegate. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The spy object reminds me of the &lt;a href="http://www.pragprog.com/the-pragmatic-programmer/extracts/software-entropy"&gt;Broken Windows&lt;/a&gt; section of the &lt;a href="http://www.amazon.com/Pragmatic-Programmer-Journeyman-Master/dp/020161622X/ref=sr_1_4?ie=UTF8&amp;amp;s=books&amp;amp;qid=1242740154&amp;amp;sr=8-4"&gt;Pragmatic Programmer&lt;/a&gt;. It clearly states not to live with broken windows, but if you cannot fix the window, at least put a board over the window. In the case of third party code where you cannot change the source code, a validation wrapper is the board you need to keep further damage from occurring and show other developers in the area that you still care about the quality of code.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;The Series&lt;/b&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting0Introduction.aspx"&gt;PatternsInTesting[0] - Introduction&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting1DummyPattern.aspx"&gt;PatternsInTesting[1] - Dummy Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;/div&gt; &lt;a href="http://developusing.net/2008/12/10/PatternsInTesting2StubPattern.aspx"&gt;PatternsInTesting[2] - Stub Pattern&lt;/a&gt;   &lt;div&gt;&lt;a href="http://developusing.net/2009/05/19/PatternsInTesting3SpyPattern.aspx"&gt;PatternsInTesting[3] - Spy Pattern&lt;/a&gt;&lt;/div&gt; PatternsInTesting[4] - Mock Pattern  &lt;img width="0" height="0" src="http://developusing.net/aggbug.ashx?id=6a0a4830-21e5-4012-a67a-51adcc156475"/&gt;&lt;/div&gt;</description>
      <comments>http://developusing.net/CommentView,guid,6a0a4830-21e5-4012-a67a-51adcc156475.aspx</comments>
      <category>patterns</category>
      <category>programming</category>
      <category>testing</category>
    </item>
    <item>
      <trackback:ping>http://developusing.net/Trackback.aspx?guid=9803fb00-0e05-4ad1-b5cb-147762ec5504</trackback:ping>
      <pingback:server>http://developusing.net/pingback.aspx</pingback:server>
      <pingback:target>http://developusing.net/PermaLink,guid,9803fb00-0e05-4ad1-b5cb-147762ec5504.aspx</pingback:target>
      <dc:creator>Dennis Burton</dc:creator>
      <wfw:comment>http://developusing.net/CommentView,guid,9803fb00-0e05-4ad1-b5cb-147762ec5504.aspx</wfw:comment>
      <wfw:commentRss>http://developusing.net/SyndicationService.asmx/GetEntryCommentsRss?guid=9803fb00-0e05-4ad1-b5cb-147762ec5504</wfw:commentRss>
      <title>More Patterns in Testing</title>
      <guid isPermaLink="false">http://developusing.net/PermaLink,guid,9803fb00-0e05-4ad1-b5cb-147762ec5504.aspx</guid>
      <link>http://developusing.net/2009/03/16/MorePatternsInTesting.aspx</link>
      <pubDate>Mon, 16 Mar 2009 17:48:40 GMT</pubDate>
      <description>&lt;div&gt;I will be presenting items from the PatternsInTesting series as well as some additional content in the &lt;a href="http://developusing.net/2009/03/12/PatternsInTestingGoesLive.aspx"&gt;Test Driven is Driving me Insane&lt;/a&gt; talk at the &lt;a href="http://migang.org/MeetingInfo.aspx?CntProv=Events&amp;amp;CntItem=918abb85-b6f6-4208-a6f5-4340db63d2b3&amp;amp;ReturnURL=%2fMeetings.aspx&amp;amp;CntCatID=a0e23ee0-1ffe-4808-8aef-4316a2f458b0"&gt;Great Lakes .net User Group&lt;/a&gt; on 3/18/2009 and at the &lt;a href="http://www.nwnug.com/"&gt;Northwest Ohio .net User Group&lt;/a&gt; on 4/21/2009. This has been a really fun talk so far and I have enjoyed the conversation it generates. Stop by if you can make it.&lt;br&gt;&lt;p&gt;&lt;/p&gt;&lt;img width="0" height="0" src="http://developusing.net/aggbug.ashx?id=9803fb00-0e05-4ad1-b5cb-147762ec5504"/&gt;&lt;/div&gt;</description>
      <comments>http://developusing.net/CommentView,guid,9803fb00-0e05-4ad1-b5cb-147762ec5504.aspx</comments>
      <category>community</category>
      <category>patterns</category>
      <category>tdd</category>
      <category>testing</category>
    </item>
    <item>
      <trackback:ping>http://developusing.net/Trackback.aspx?guid=ea980c5a-24ab-4e03-92eb-00685ad0b38b</trackback:ping>
      <pingback:server>http://developusing.net/pingback.aspx</pingback:server>
      <pingback:target>http://developusing.net/PermaLink,guid,ea980c5a-24ab-4e03-92eb-00685ad0b38b.aspx</pingback:target>
      <dc:creator>Dennis Burton</dc:creator>
      <wfw:comment>http://developusing.net/CommentView,guid,ea980c5a-24ab-4e03-92eb-00685ad0b38b.aspx</wfw:comment>
      <wfw:commentRss>http://developusing.net/SyndicationService.asmx/GetEntryCommentsRss?guid=ea980c5a-24ab-4e03-92eb-00685ad0b38b</wfw:commentRss>
      <title>Patterns in testing goes live</title>
      <guid isPermaLink="false">http://developusing.net/PermaLink,guid,ea980c5a-24ab-4e03-92eb-00685ad0b38b.aspx</guid>
      <link>http://developusing.net/2009/03/12/PatternsInTestingGoesLive.aspx</link>
      <pubDate>Thu, 12 Mar 2009 13:15:15 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;I will be presenting items from the PatternsInTesting series&amp;nbsp;at the &lt;a href="http://glugnet.org"&gt;Greater 
Lansing .net User Group Flint &lt;/a&gt;meeting. I was compelled to put this blog series 
and presentation together to address the pain many organization experience when 
trying to include automated testing into their development process. The content 
is based on the insight and lessons learned that I have picked up by 
experiencing the same transition in multiple organizations. Participants in this 
presentation will walk away with tools for writing more effective tests and how 
to better identify issues in tests.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;img width="0" height="0" src="http://developusing.net/aggbug.ashx?id=ea980c5a-24ab-4e03-92eb-00685ad0b38b"/&gt;&lt;/div&gt;</description>
      <comments>http://developusing.net/CommentView,guid,ea980c5a-24ab-4e03-92eb-00685ad0b38b.aspx</comments>
      <category>community</category>
      <category>patterns</category>
      <category>tdd</category>
      <category>testing</category>
    </item>
    <item>
      <trackback:ping>http://developusing.net/Trackback.aspx?guid=7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5</trackback:ping>
      <pingback:server>http://developusing.net/pingback.aspx</pingback:server>
      <pingback:target>http://developusing.net/PermaLink,guid,7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5.aspx</pingback:target>
      <dc:creator>Dennis Burton</dc:creator>
      <wfw:comment>http://developusing.net/CommentView,guid,7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5.aspx</wfw:comment>
      <wfw:commentRss>http://developusing.net/SyndicationService.asmx/GetEntryCommentsRss?guid=7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5</wfw:commentRss>
      <title>PatternsInTesting[2] - Stub Pattern</title>
      <guid isPermaLink="false">http://developusing.net/PermaLink,guid,7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5.aspx</guid>
      <link>http://developusing.net/2008/12/10/PatternsInTesting2StubPattern.aspx</link>
      <pubDate>Wed, 10 Dec 2008 22:49:06 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;strong&gt;The Scenario&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;On our continuing quest to create unit tests that exercise only the class under test, we look at another common scenario that occurs while writing tests. As with the &lt;i&gt;Dummy&lt;/i&gt;, our class under test has a dependency on another class, only this time the dependent class has an active role in our test. Our testing needs are about the logic of the class under test and not the interaction with the dependent object. In order to create a good test, the class under test must be isolated from the dependent object. &lt;/p&gt;  &lt;p&gt;An example of this scenario might look like this: &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICalculator&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;int&lt;/span&gt; Add(&lt;span style="color: blue"&gt;int&lt;/span&gt; left, &lt;span style="color: blue"&gt;int&lt;/span&gt; right);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fib&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICalculator&lt;/span&gt; _calculator;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICalculator&lt;/span&gt; Calculator&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 19&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; _calculator; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set&lt;/span&gt; { _calculator = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 23&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; Next(&lt;span style="color: blue"&gt;int&lt;/span&gt; i, &lt;span style="color: blue"&gt;int&lt;/span&gt; j)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { &lt;span style="color: blue"&gt;return&lt;/span&gt; Calculator.Add(i,j); }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The class under test in this scenario is &lt;i&gt;Fib&lt;/i&gt;, which has a dependency on an &lt;i&gt;ICalculator&lt;/i&gt;. The test's objective is to validate that the &lt;i&gt;Next&lt;/i&gt; method returns the correct result for some well-known examples.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Vocabulary&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The name of this pattern is the Stub. A stub stands in the place of the actual object in use and provides known answers and predictable behavior. If you are doing any sort of evolutionary development, chances are that the initial versions of your classes more resemble stubs then real code. Why? The goal is the same: you wrote stub functionality to allow you to focus your development efforts on different parts of the system. This is exactly what we are doing with Stub tests: isolate one part of the system from another by providing known results. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The solution without tools&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Unlike with a &lt;i&gt;Dummy&lt;/i&gt;, providing a class that throws a &lt;i&gt;NotImplementedExcpetion&lt;/i&gt; in the &lt;i&gt;Add&lt;/i&gt; method does not meet our needs. Since the functionality of &lt;i&gt;ICalculator&lt;/i&gt; is outside the scope of this test, we assume that it is working correctly (and hopefully under test). A simplistic implementation of &lt;i&gt;ICalculator &lt;/i&gt;will work nicely. Since we are not testing the calculator, provide a simplistic calculator that returns fixed results.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 38&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 39&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NextResultIsCorrect()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 40&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 41&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Fib&lt;/span&gt; fib = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fib&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 42&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fib.Calculator = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StubCalculator&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 43&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 44&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.That(fib.Next(2, 3), &lt;span style="color: #2b91af"&gt;Is&lt;/span&gt;.EqualTo(5));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 45&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 29&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StubCalculator&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;ICalculator&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 30&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 31&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt; Add(&lt;span style="color: blue"&gt;int&lt;/span&gt; left, &lt;span style="color: blue"&gt;int&lt;/span&gt; right)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 32&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { &lt;span style="color: blue"&gt;return&lt;/span&gt; 5; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 33&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The test will check that the &lt;i&gt;Next &lt;/i&gt;method returns the correct result given the &lt;i&gt;StubCalculator&lt;/i&gt;. What we end up doing here is fully exercising the &lt;i&gt;Fib &lt;/i&gt;class with known values from its dependent classes. The stub gives us the proper level of isolation for this test.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The solution with Rhino Mocks&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;For this version, leverage Rhino Mocks to keep us from having to code physical versions of the Stub class. Using Rhino's Fluent Interface, this reads as Expect a call on Calculator with the parameters 2 and 3, and when making this call return 5 as a result.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 47&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 48&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; NextNumberIsCorrect()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 49&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 50&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Fib&lt;/span&gt; fib = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Fib&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 51&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fib.Calculator = &lt;span style="color: #2b91af"&gt;MockRepository&lt;/span&gt;.GenerateStub&amp;lt;&lt;span style="color: #2b91af"&gt;ICalculator&lt;/span&gt;&amp;gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 52&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; fib.Calculator.Expect(calc =&amp;gt; calc.Add(2, 3)).Return(5);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 53&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 54&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.That(fib.Next(2, 3), &lt;span style="color: #2b91af"&gt;Is&lt;/span&gt;.EqualTo(5));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;&amp;#160;&amp;#160; 55&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;The tools advantage&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Just as discussed with &lt;i&gt;IComplicated&lt;/i&gt; in the &lt;i&gt;Dummy &lt;/i&gt;sample, adding methods to &lt;i&gt;ICalculator&lt;/i&gt; does not require any additional maintenance of this test. However, unlike the &lt;i&gt;Dummy&lt;/i&gt; sample, calls into the stub return the expected value. Additional benefits can easily pile up. Consider adding multiple calls to &lt;i&gt;Add &lt;/i&gt;with different parameters. The hand-coded version would need some sort of conditional logic to determine what to return based on the calling parameters. Complexity adds up fast, even in the simple example listed here. Using Rhino, one concise and readable line of code can use new parameters for an additional expectation, including the expected result, and Rhino deals with matching up the parameters with the correct result. This is just a glimpse into the functionality offered by Rhino; the upcoming patterns will cover even more capability.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Isolation effects&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Should a test that checks the result of the &lt;i&gt;Next &lt;/i&gt;method fail if there is something wrong with the &lt;i&gt;ICalculator &lt;/i&gt;implementation? The answer, as always, is &amp;quot;it depends.&amp;quot; This test should fail if we were writing an integration test—a test that ensures all the pieces of a system are working together. This test should not fail based on &lt;i&gt;ICalculator &lt;/i&gt;if it is a unit test, focused only on the result of &lt;i&gt;Next&lt;/i&gt;. Isolating &lt;i&gt;ICalculator &lt;/i&gt;from &lt;i&gt;Fib &lt;/i&gt;helps build a set of unit tests that can quickly identify the location of errors introduced into a system. The stub is a common pattern of isolation, and using it will make a marked and immediate improvement in your tests.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;The Series&lt;/b&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting0Introduction.aspx"&gt;PatternsInTesting[0] - Introduction&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting1DummyPattern.aspx"&gt;PatternsInTesting[1] - Dummy Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/10/PatternsInTesting2StubPattern.aspx"&gt;PatternsInTesting[2] - Stub Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2009/05/19/PatternsInTesting3SpyPattern.aspx"&gt;PatternsInTesting[3] - Spy Pattern&lt;/a&gt;&lt;/div&gt; PatternsInTesting[4] - Mock Pattern  &lt;img width="0" height="0" src="http://developusing.net/aggbug.ashx?id=7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5"/&gt;&lt;/div&gt;</description>
      <comments>http://developusing.net/CommentView,guid,7a8184a8-d343-4b6e-9c8d-70b95e8e9ab5.aspx</comments>
      <category>patterns</category>
      <category>programming</category>
      <category>testing</category>
    </item>
    <item>
      <trackback:ping>http://developusing.net/Trackback.aspx?guid=21f508fe-a715-4d6c-8105-c58cd81b0a1f</trackback:ping>
      <pingback:server>http://developusing.net/pingback.aspx</pingback:server>
      <pingback:target>http://developusing.net/PermaLink,guid,21f508fe-a715-4d6c-8105-c58cd81b0a1f.aspx</pingback:target>
      <dc:creator>Dennis Burton</dc:creator>
      <wfw:comment>http://developusing.net/CommentView,guid,21f508fe-a715-4d6c-8105-c58cd81b0a1f.aspx</wfw:comment>
      <wfw:commentRss>http://developusing.net/SyndicationService.asmx/GetEntryCommentsRss?guid=21f508fe-a715-4d6c-8105-c58cd81b0a1f</wfw:commentRss>
      <title>PatternsInTesting[1] - Dummy Pattern</title>
      <guid isPermaLink="false">http://developusing.net/PermaLink,guid,21f508fe-a715-4d6c-8105-c58cd81b0a1f.aspx</guid>
      <link>http://developusing.net/2008/12/04/PatternsInTesting1DummyPattern.aspx</link>
      <pubDate>Thu, 04 Dec 2008 10:01:09 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;&lt;strong&gt;The Scenario&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One of the characteristics of a good &lt;i&gt;unit&lt;/i&gt; test is that the object under test is the only object being exercised. The problem in this scenario is that the object under test requires a dependent object, even though the functionality of the dependent object is not used in the test. To make a good test, the dependent object needs to be isolated from the class under test. &lt;/p&gt;  &lt;p&gt;An example of this scenario might look like this: &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 8&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IAmComplicated&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 9&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; { &lt;span style="color: blue"&gt;void&lt;/span&gt; DoStuff(); }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 10&lt;/span&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 11&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ClassUnderTest&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 12&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 13&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IAmComplicated&lt;/span&gt; complicated;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 14&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: rgb(43,145,175)"&gt;double&lt;/span&gt; circumference;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 15&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font color="#0000ff"&gt;public&lt;/font&gt;&amp;#160;&lt;font color="#2b91af"&gt;double&lt;/font&gt; radius;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 16&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; ClassUnderTest(&lt;span style="color: rgb(43,145,175)"&gt;IAmComplicated&lt;/span&gt; externalComplicated)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 17&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 18&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt;( externalComplicated == &lt;span style="color: blue"&gt;null&lt;/span&gt; ) &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 19&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;complicated is required&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 20&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; complicated = externalComplicated; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 21&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 22&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;double&lt;/span&gt; DoInternalStuff() &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 23&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 24&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; circumference/(2*radius);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 25&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 26&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The dependency in &lt;i&gt;ClassUnderTest&lt;/i&gt; is that its constructor requires an instance of an object that implements &lt;i&gt;IAmComplicated&lt;/i&gt;.&lt;i&gt; &lt;/i&gt;The rather simple objective of this test is to validate that DoInternalStuff returns Pi within a reasonable amount of rounding error.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Vocabulary&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The name of this pattern is the Dummy. It is unclear to me whether this is a reference to the object that enables isolation for the test or a reference to the original author of the code. It seems to be a code smell for this scenario to even occur. However, sometimes you need to use an external library and do not have liberty of changing the code.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The solution without tools&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Using only Visual Studio, add a class that implements &lt;i&gt;IAmComplicated&lt;/i&gt;, right-click on it, and choose implement interface. Every method in the class will throw a &lt;i&gt;NotImplementedException&lt;/i&gt;. This class meets the needs of the test because none of the methods are ever called; its only purpose is existence.This is your Dummy.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 1&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ComplicatedDummy&lt;/span&gt; : &lt;span style="color: rgb(43,145,175)"&gt;IAmComplicated&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 2&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 3&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DoStuff()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 4&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 5&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;NotImplementedException&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 6&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160;&amp;#160; 7&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This Dummy class allows us to create the following test: &lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 39&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: rgb(43,145,175)"&gt;Test&lt;/span&gt;]&lt;/p&gt;    &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 40&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DoStuffValidates()&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 41&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 42&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: rgb(43,145,175)"&gt;ClassUnderTest&lt;/span&gt; cut = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ClassUnderTest&lt;/span&gt;(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ComplicatedDummy&lt;/span&gt;());&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 43&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cut.circumference = 314;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 44&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cut.radius = 50;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 45&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: rgb(43,145,175)"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: rgb(43,145,175)"&gt;Math&lt;/span&gt;.PI, cut.DoInternalStuff(), 0.01d);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 46&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;The solution with Rhino Mocks&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Rhino Mocks has the capability to create an object at runtime by reflecting the &lt;i&gt;IAmComplicated&lt;/i&gt; interface. This gives us the capability we need without having to maintain another class in the test project. Since Rhino is reflecting the interface at runtime, adding a method to the interface at a later date does not require changes to the test code. There are several different ways Rhino could give us a placeholder for &lt;i&gt;IAmComplicated&lt;/i&gt;. Here we will use a simple one line call to &lt;i&gt;GenerateStub&lt;/i&gt;.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 55&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: rgb(43,145,175)"&gt;MockRepository&lt;/span&gt;.GenerateStub&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;IAmComplicated&lt;/span&gt;&amp;gt;();&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This one line saves us an entire file of maintenance, and our test only requires minor modification to use this technique. Place the call to Rhino as the parameter to the &lt;i&gt;ClassUnderTest&lt;/i&gt; constructor instead of creating a new &lt;i&gt;ComplicatedDummy&lt;/i&gt;.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 52&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [&lt;span style="color: rgb(43,145,175)"&gt;Test&lt;/span&gt;]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 53&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DoStuffValidates()&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 55&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: rgb(43,145,175)"&gt;ClassUnderTest&lt;/span&gt; cut = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;ClassUnderTest&lt;/span&gt;(&lt;span style="color: rgb(43,145,175)"&gt;MockRepository&lt;/span&gt;.GenerateStub&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;IAmComplicated&lt;/span&gt;&amp;gt;());&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 56&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cut.circumference = 314;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 57&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cut.radius = 50;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 58&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: rgb(43,145,175)"&gt;Assert&lt;/span&gt;.AreEqual(&lt;span style="color: rgb(43,145,175)"&gt;Math&lt;/span&gt;.PI, cut.DoInternalStuff(), 0.01d);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: rgb(43,145,175)"&gt;&amp;#160;&amp;#160; 59&lt;/span&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt; &lt;/div&gt;  &lt;p&gt;&lt;strong&gt;Isolation of Failure&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;One of the primary goals of good unit tests is to identify exactly which unit caused the error. By applying this pattern, tests that cover &lt;i&gt;ClassUnderTest&lt;/i&gt; no longer require an implementation of &lt;i&gt;IAmComplicated&lt;/i&gt; to instantiate without failure. This helps shorten your bug hunting cycle by reducing the areas that indicate error to where the real error occurred. As you attain higher levels of isolation and better definition of units, you will find you spend much less time in the debugger. This means spending less time finding problems more time fixing them.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;The Series&lt;/b&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting0Introduction.aspx"&gt;PatternsInTesting[0] - Introduction&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting1DummyPattern.aspx"&gt;PatternsInTesting[1] - Dummy Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;/div&gt; &lt;a href="http://developusing.net/2008/12/10/PatternsInTesting2StubPattern.aspx"&gt;PatternsInTesting[2] - Stub Pattern&lt;/a&gt;   &lt;div&gt;&lt;a href="http://developusing.net/2009/05/19/PatternsInTesting3SpyPattern.aspx"&gt;PatternsInTesting[3] - Spy Pattern&lt;/a&gt;&lt;/div&gt; PatternsInTesting[4] - Mock Pattern  &lt;img width="0" height="0" src="http://developusing.net/aggbug.ashx?id=21f508fe-a715-4d6c-8105-c58cd81b0a1f"/&gt;&lt;/div&gt;</description>
      <comments>http://developusing.net/CommentView,guid,21f508fe-a715-4d6c-8105-c58cd81b0a1f.aspx</comments>
      <category>patterns</category>
      <category>programming</category>
      <category>testing</category>
    </item>
    <item>
      <trackback:ping>http://developusing.net/Trackback.aspx?guid=ba591026-21f9-451e-ae23-76d401194b36</trackback:ping>
      <pingback:server>http://developusing.net/pingback.aspx</pingback:server>
      <pingback:target>http://developusing.net/PermaLink,guid,ba591026-21f9-451e-ae23-76d401194b36.aspx</pingback:target>
      <dc:creator>Dennis Burton</dc:creator>
      <wfw:comment>http://developusing.net/CommentView,guid,ba591026-21f9-451e-ae23-76d401194b36.aspx</wfw:comment>
      <wfw:commentRss>http://developusing.net/SyndicationService.asmx/GetEntryCommentsRss?guid=ba591026-21f9-451e-ae23-76d401194b36</wfw:commentRss>
      <title>PatternsInTesting[0] - Introduction</title>
      <guid isPermaLink="false">http://developusing.net/PermaLink,guid,ba591026-21f9-451e-ae23-76d401194b36.aspx</guid>
      <link>http://developusing.net/2008/12/04/PatternsInTesting0Introduction.aspx</link>
      <pubDate>Thu, 04 Dec 2008 03:05:47 GMT</pubDate>
      <description>&lt;div&gt;&lt;p&gt;It does not matter if your test philosophy is Test Driven Development, Test Eventually Development, or I write tests occasionally. Very early in your testing experience, you will want to start isolating portions of your application or libraries. Without this isolation, you will start seeing a fragile set of tests that make it challenging to track down the point of failure. In order to reach the level of isolation that your tests need, you will need to introduce Mocks (or Test Doubles) to your testing toolbox.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Testing Story&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Someone on your team (extra geek cred if it was you) brings the concept of adding tests to the development process. No doubt there is some confusion at first. What are tests? How do we write them? This phase quickly passes as the team finds things that they understand how to test. This initial period of enlightenment yields great results. Everyone is happy that code is doing what it should be doing. Occasionally, the team celebrates when a broken test flags an unintended change. Clearly development will never be the same again. &lt;/p&gt;  &lt;p&gt;As time goes on and the test library gets larger, you start to notice that a breaking change often causes quite a few tests to fail. The team finds this annoying, but decides having tests is better than developer life before tests. At some point though, the tests start to feel burdensome. Adding simple changes start to propagate through the whole system of tests. Discouragement and discontentment set in, and some of your team members want to abandon developing with tests. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Enter Mocks&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;At this point, the prominent options are to ignore the tests (the blue pill) or find a way to make the tests better (the red pill). This series will cover some of the test development patterns used to isolate areas of your code in the hopes that you will choose the red pill. The goal for the initial four or five posts will be to cover the types of scenarios that occur in test code and what common solutions look like. After that, I hope to cover some of the patterns for dealing with legacy code as I am currently reading through Working Effectively with Legacy Code and it seems relevant to the client I am working with.&lt;/p&gt;  &lt;p&gt;&lt;b&gt;The Mock Controversy&lt;/b&gt; &lt;/p&gt;  &lt;p&gt;There has been considerable conversation in the community about the usage of the term &lt;i&gt;mock&lt;/i&gt;. Some prominent members of the development community have been pushing for changing the vocabulary to &lt;i&gt;test doubles&lt;/i&gt;, where &lt;i&gt;mock&lt;/i&gt;, &lt;i&gt;stub&lt;/i&gt;, &lt;i&gt;fake&lt;/i&gt; and &lt;i&gt;spy&lt;/i&gt; are a particular type of &lt;i&gt;test double&lt;/i&gt;s. My take on this is that these &lt;i&gt;test double&lt;/i&gt; types are each describing the scenario where it is useful and the technique for solving the problem; this is describing a pattern. Good names are the key to effective patterns, and we have a good set of names for pattern description. It really does not matter what you call the concept (&lt;i&gt;mocking&lt;/i&gt; | &lt;i&gt;doubling&lt;/i&gt; | &lt;i&gt;faking | test doubles&lt;/i&gt;) if the pattern names describe the scenario effectively. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;What's next?&lt;/b&gt; &lt;/p&gt;  &lt;p&gt;This Series of posts will explore these test double patterns and decompose a few alternate implementations. &lt;/p&gt;  &lt;p&gt;&lt;b&gt;The Series&lt;/b&gt;    &lt;br /&gt;&lt;/p&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting0Introduction.aspx"&gt;PatternsInTesting[0] - Introduction&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/04/PatternsInTesting1DummyPattern.aspx"&gt;PatternsInTesting[1] - Dummy Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2008/12/10/PatternsInTesting2StubPattern.aspx"&gt;PatternsInTesting[2] - Stub Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;&lt;a href="http://developusing.net/2009/05/19/PatternsInTesting3SpyPattern.aspx"&gt;PatternsInTesting[3] - Spy Pattern&lt;/a&gt;&lt;/div&gt;  &lt;div&gt;PatternsInTesting[4] - Mock Pattern&lt;/div&gt;&lt;img width="0" height="0" src="http://developusing.net/aggbug.ashx?id=ba591026-21f9-451e-ae23-76d401194b36"/&gt;&lt;/div&gt;</description>
      <comments>http://developusing.net/CommentView,guid,ba591026-21f9-451e-ae23-76d401194b36.aspx</comments>
      <category>patterns</category>
      <category>programming</category>
      <category>testing</category>
    </item>
  </channel>
</rss>