<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.bespoke.com.my/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>agile : TDD</title><link>http://blogs.bespoke.com.my/blogs/agile/archive/tags/TDD/default.aspx</link><description>Tags: TDD</description><dc:language>en</dc:language><generator>CommunityServer 2.1 (Build: 60809.935)</generator><item><title>To fake it or not</title><link>http://blogs.bespoke.com.my/blogs/agile/archive/2006/10/04/To-fake-it-or-not.aspx</link><pubDate>Tue, 03 Oct 2006 06:57:00 GMT</pubDate><guid isPermaLink="false">9c7185d0-067c-41a7-bca3-6488d40287f0:8</guid><dc:creator>erymuzuan</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.bespoke.com.my/blogs/agile/comments/8.aspx</comments><wfw:commentRss>http://blogs.bespoke.com.my/blogs/agile/commentrss.aspx?PostID=8</wfw:commentRss><description>&lt;p&gt;Continuation from my 1st blog about Fake/Mock object. With the availbility of Dependency Injection framework(Microsoft Pattern and Practice ObjectBuilder, Spring Framewrok and StructureMap). We could easily swicth the implemetation of our interface. In my opinion creating a Mock just for the sake of it, just going to make your code less readable i.e. it&amp;#39;s going to cost more on the maintanance. &lt;/p&gt;&lt;p&gt;&amp;nbsp;This is how we would normally work when dealing with a dependency that we didn&amp;#39;t really care about. Just as exmple we have a domain object called Patient, it has one operation , Save() returs void, the idea is when we call Save on the Patient instance, it must do some validation, then if the patientID is not null , it&amp;#39;s new item and we want to add a new item to our persintance, if PatientID is not null, is mean we&amp;#39;re updating existing patient.&lt;/p&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="rem"&gt;// our test case&lt;/span&gt;
        [Test]
        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SavePatient()
        {

            Patient p = &lt;span class="kwrd"&gt;new&lt;/span&gt; Patient();
            p.Save();

            Assert.AreEqual(p.PatientID, 30);

        }



        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; Patient
        {

            &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;int&lt;/span&gt; PatientID { set; get;}

            &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Save()
            {

                &lt;span class="kwrd"&gt;if&lt;/span&gt; (&lt;span class="kwrd"&gt;this&lt;/span&gt;.Validator.Validate(&lt;span class="kwrd"&gt;this&lt;/span&gt;)) &lt;span class="kwrd"&gt;
               throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentException(&lt;span class="str"&gt;&amp;quot;Validation error&amp;quot;&lt;/span&gt;);
                &lt;span class="kwrd"&gt;if&lt;/span&gt; (PatientID &amp;gt; 0)
                {
                    &lt;span class="kwrd"&gt;this&lt;/span&gt;.Persistance.Update(&lt;span class="kwrd"&gt;this&lt;/span&gt;);

                }
                &lt;span class="kwrd"&gt;else&lt;/span&gt;
                {
                    &lt;span class="kwrd"&gt;this&lt;/span&gt;.PatientID = &lt;span class="kwrd"&gt;this&lt;/span&gt;.Persistance.Add(&lt;span class="kwrd"&gt;this&lt;/span&gt;);

                }
            }
        }&lt;/pre&gt;&lt;p&gt;So there are a couple of dependecies that we have here in order to make this method works , the Persistace and Validator, so what do we want to do is to test the domain Save method , not the Persistance or Validator. So the instinct is to do some refactoring , the first one is &amp;quot;Extract Interface&amp;quot; ( please refer to Martin Fowler -Refactoring for more refactoring details, I also a bif fan of Refactoring to Pattern by Joshue Krienvesky).&amp;nbsp; Instead of having a concrete class for Validator and Persistance we now rever it to their interface&lt;/p&gt;&lt;pre class="csharpcode"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IValidator
        {
            &lt;span class="kwrd"&gt;bool&lt;/span&gt; Validate(Patient patient);
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IPersistance
        {
            &lt;span class="kwrd"&gt;int&lt;/span&gt; Save(Patient patient);
        }
        &lt;span class="rem"&gt;// thus our patient object&lt;/span&gt;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; IValidator m_validator;
        &lt;span class="kwrd"&gt;private&lt;/span&gt; IPersistance m_persistance;

        &lt;span class="kwrd"&gt;public&lt;/span&gt; IPersistance Persistance
        {
            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; m_persistance; }
            set { m_persistance = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }
        }

        &lt;span class="kwrd"&gt;public&lt;/span&gt; IValidator Validator
        {
            get { &lt;span class="kwrd"&gt;return&lt;/span&gt; m_validator; }
            set { m_validator = &lt;span class="kwrd"&gt;value&lt;/span&gt;; }
        }
&lt;/pre&gt;&lt;img src="http://blogs.bespoke.com.my/aggbug.aspx?PostID=8" width="1" height="1"&gt;</description><category domain="http://blogs.bespoke.com.my/blogs/agile/archive/tags/TDD/default.aspx">TDD</category></item></channel></rss>