<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-8412831559140848595</atom:id><lastBuildDate>Sat, 18 May 2013 18:27:55 +0000</lastBuildDate><category>NUnit</category><category>Resharper</category><category>AOP</category><category>PostSharp</category><category>Performance</category><category>nDepend</category><category>TFS</category><category>Podcast</category><category>ALT.NET</category><category>CI</category><category>Thoughts</category><category>VB.NET</category><category>F#</category><category>Mock objects</category><category>MSTest</category><category>Typemock</category><category>Refactoring</category><category>Visual Studio 2010</category><category>Open Source</category><category>Blogging</category><category>C++</category><category>C#</category><category>XUnit</category><category>Reflection</category><category>Productivity</category><category>Multi-threading</category><category>TDD</category><category>Unit tests</category><category>Git</category><category>Agile</category><category>BDD</category><category>DSL</category><category>Book review</category><category>Conference</category><category>Scrum</category><category>Better Place</category><category>Tools</category><category>IronRuby</category><category>Presentation</category><category>.NET 4</category><category>Software Craftmanship</category><category>IronPython</category><category>FakeItEasy</category><category>DirectX</category><category>WPF</category><category>Tips and Tricks</category><category>.NET</category><title>Helper Code</title><description>Dror Helper's Blog</description><link>http://blog.drorhelper.com/</link><managingEditor>noreply@blogger.com (Dror Helper)</managingEditor><generator>Blogger</generator><openSearch:totalResults>202</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/HelperCode" /><feedburner:info uri="helpercode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><image><link>blog.drorhelper.com</link><url>http://blogs.microsoft.co.il/blogs/dhelper/Art/logo_small.png</url><title>Helper Code</title></image><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-1386949024286999465</guid><pubDate>Wed, 15 May 2013 19:35:00 +0000</pubDate><atom:updated>2013-05-17T08:25:41.116+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">FakeItEasy</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Unit tests</category><category domain="http://www.blogger.com/atom/ns#">Tips and Tricks</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><category domain="http://www.blogger.com/atom/ns#">TDD</category><category domain="http://www.blogger.com/atom/ns#">Mock objects</category><title>How to write a unit test</title><description>Last week I had the pleasure of participating in &lt;a href="http://blog.drorhelper.com/2013/04/sela-developer-practice-2013.html" target="_blank"&gt;Sela Developer Practice&lt;/a&gt;. &lt;br /&gt;
Before my session I sat through &lt;a href="http://www.gilzilberfeld.com/" target="_blank"&gt;Gil ZIlberfeld&lt;/a&gt;’s session “&lt;a href="http://www.gilzilberfeld.com/2013/05/7-steps-for-writing-your-first-test.html" target="_blank"&gt;7 steps for writing your first unit test&lt;/a&gt;” and I found myself thinking – what are steps I take when writing a new unit test?&lt;br /&gt;
I’ve been writing them for so long and never noticed I’ve been following a “methodology” of sort. And so without further ado – here is the steps &lt;strong&gt;I&lt;/strong&gt; take when writing a new unit test:&lt;br /&gt;
&lt;h3&gt;
The code under test&lt;/h3&gt;
In order to write a unit test I’ll need an example and so&amp;nbsp; I came up with the following scenario:&lt;br /&gt;
&lt;blockquote&gt;
This is the latest and greatest bug tracking software and we need to add a feature – send an email when a bug with certain severity is created.&lt;/blockquote&gt;
The code we want to test looks something (read: exactly) like this:&lt;br /&gt;
&lt;pre class="brush: csharp;"&gt;public class BugTracker
{
    private readonly IBugRepository _repository;
    private readonly IEmailClient _emailClient;

    public BugTracker(IBugRepository repository, IEmailClient emailClient)
    {
        _repository = repository;
        _emailClient = emailClient;
    }

    public Bug CreatNewBug(string title, Severity severity)
    {
        if (string.IsNullOrEmpty(title))
        {
            throw new ApplicationException("Title cannot be empty");
        }

        var newBug = new Bug
             {
                 Title = title,
                 Severity = severity
             };

        SaveBugToDb(newBug);

        // Here be code        

        return newBug;
    }&lt;/pre&gt;
&lt;br /&gt;
And so since we’re avid TDD (Test Driven Design) practitioners we’ll start by writing the test first.&lt;br /&gt;
&lt;h3&gt;
Decide what you’re testing&lt;/h3&gt;
Although this might sound trivial – deciding what to test is a step that many developers tend to forget - instead they write a chunk of code and assert whatever they can.&lt;br /&gt;
I found that naming the test method in such a way that forces me (and my fellow developers) to think about what they are about to do:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: [5];"&gt;[TestFixture]
public class BugTrackerTests
{
    [Test]
    public void CreatNewBug_CreateBugHasHighestSeverity_SendEmailToProjectManager()
    {
        
    }
}&lt;/pre&gt;
&lt;br /&gt;
I didn't invent this naming convention but I find it very useful. The name is divided into three parts:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;The method I’m testing - not description, scenario just the name of the method&lt;/li&gt;
&lt;li&gt;The scenario I’m testing&lt;/li&gt;
&lt;li&gt;What I expect to happen&lt;/li&gt;
&lt;/ol&gt;
The added benefit is that when this test would fail – all I need is to read the name of the test to know what went wrong – take that F5!&lt;br /&gt;
There are other similar naming schemas – choose one and be persistent about it.&lt;br /&gt;
So now I know what I’m about to test and the next step is to write exactly that.&lt;br /&gt;
&lt;h3&gt;
Write the method under test&lt;/h3&gt;
I’m starting from the bare minimum and building my test from the inside out.&lt;br /&gt;
First I’ll create the class I’m testing and then I’ll run the method I’m testing.&lt;br /&gt;
&lt;pre class="brush: csharp;"&gt;[Test]
public void CreatNewBug_CreateBugHasHighestSeverity_SendEmailToProjectManager()
{
    var cut = new BugTracker();

    cut.CreatNewBug("my title", Severity.OhMyGod);
}&lt;/pre&gt;
Unfortunately this does not even compile. The problem is that I need to “feed” the &lt;em&gt;BugTracker&lt;/em&gt; class two dependencies of type &lt;em&gt;IBugRepository&lt;/em&gt; and &lt;em&gt;IEmailClient&lt;/em&gt; – so let’s add them courtesy of an Isolation framework (in this case FakeItEasy):&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: [7];"&gt;[Test]
public void CreatNewBug_CreateBugHasHighestSeverity_SendEmailToProjectManager()
{
    var fakeBugRepository = A.Fake&amp;lt;IBugRepository&amp;gt;();
    var fakeEmailClient = A.Fake&amp;lt;IEmailClient&amp;gt;();

    var cut = new BugTracker(fakeBugRepository, fakeEmailClient);

    cut.CreatNewBug("my title", Severity.OhMyGod);
}&lt;/pre&gt;
And now we can write the actual assertion.&lt;br /&gt;
&lt;h3&gt;
Write the assertion&lt;/h3&gt;
Since we need to check that our email client has sent a message we need to use the power of our isolation framework to assert exactly that&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: [11];"&gt;[Test]
public void CreatNewBug_CreateBugHasHighestSeverity_SendEmailToProjectManager()
{
    var fakeBugRepository = A.Fake&amp;lt;IBugRepository&amp;gt;();
    var fakeEmailClient = A.Fake&amp;lt;IEmailClient&amp;gt;();

    var cut = new BugTracker(fakeBugRepository, fakeEmailClient);

    cut.CreatNewBug("my title", Severity.OhMyGod);

    A.CallTo(() =&amp;gt; fakeEmailClient.Send("manager@project.com", "Don't Panic!")).MustHaveHappened();
}&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Run the test&lt;/h3&gt;
Now that we have all the parts placed it’s time to run the test and see it fails.&lt;br /&gt;
The test did fail but from the wrong reasons:&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/-UqqJX33sito/UZPjljmTjWI/AAAAAAAADYI/NA7ausZKYdc/s1600-h/image%25255B13%25255D.png"&gt;&lt;img alt="image" border="0" height="417" src="http://lh6.ggpht.com/-BGyj_uL6vTU/UZPjmk84s6I/AAAAAAAADYM/gx8d-FjtnBM/image_thumb%25255B7%25255D.png?imgmax=800" style="background-image: none; border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="494" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
It appear I have a code inside the method “SaveBugToDb” that throws an exception:&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: [3];"&gt;private void SaveBugToDb(Bug newBug)
{
    if (!_repository.Connected)
    {
        throw new ApplicationException("Cannot access bug repository");
    }

    _repository.Save(newBug);
}&lt;/pre&gt;
This means going back to the drawing board for us.&lt;br /&gt;
&lt;h3&gt;
Add more code&lt;/h3&gt;
In order to make the test fail from the correct reason I’ll add one more line courtesy of our Isolation framework to make sure that a call to &lt;em&gt;Connected&lt;/em&gt; will always return true:&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: [5];"&gt;[Test]
public void CreatNewBug_CreateBugHasHighestSeverity_SendEmailToProjectManager()
{
    var fakeBugRepository = A.Fake&amp;lt;IBugRepository&amp;gt;();
    A.CallTo(() =&amp;gt; fakeBugRepository.Connected).Returns(true);

    var fakeEmailClient = A.Fake&amp;lt;IEmailClient&amp;gt;();

    var cut = new BugTracker(fakeBugRepository, fakeEmailClient);

    cut.CreatNewBug("my title", Severity.OhMyGod);

    A.CallTo(() =&amp;gt; fakeEmailClient.Send("manager@project.com", "Don't Panic!")).MustHaveHappened();
}&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Run the test (again)&lt;/h3&gt;
No I run the test again and it fails on the assertion. If it wasn’t the case I would go back and add more code to make sure that the test follow the correct path until I’m satisfied that I’m testing the correct thing.&lt;br /&gt;
&lt;h3&gt;
Write the code to make the test pass &lt;/h3&gt;
This one is simple – just add the code that makes the test pass:&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: [16,17,18,19];"&gt;public Bug CreatNewBug(string title, Severity severity)
{
    if (string.IsNullOrEmpty(title))
    {
        throw new ApplicationException("Title cannot be empty");
    }

    var newBug = new Bug
         {
             Title = title,
             Severity = severity
         };

    SaveBugToDb(newBug);

    if (severity == Severity.OhMyGod)
    {
        _emailClient.Send("manager@project.com", "Don't Panic!");
    }

    return newBug;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Conclusion&lt;/h3&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;So what did we do:&lt;/li&gt;
&lt;li&gt;Decided what to test&lt;/li&gt;
&lt;li&gt;Write the method under test&lt;/li&gt;
&lt;li&gt;Add assertion&lt;/li&gt;
&lt;li&gt;Run the test&lt;/li&gt;
&lt;li&gt;Add more code&lt;/li&gt;
&lt;li&gt;Repeat steps 4,5 if necessary&lt;/li&gt;
&lt;li&gt;Write the code that makes the test pass&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;ol&gt;&lt;br /&gt;&lt;/ol&gt;
A few comments before the end:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;This is nothing new if you’re been writing unit tests in the past – you’ve probably followed a similar process – I just needed to write is down explicitly.&lt;/li&gt;
&lt;li&gt;It seems like a lot of steps just to create a single test? Don’t despair - writing all of them should not take too long and besides – can you write a test without going through all of them in one way or another.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.drorhelper.com/2010/08/tdd-is-more-than-just-writing-unit.html" target="_blank"&gt;I'm a true believer of writing the tests before the code&lt;/a&gt; but don’t worry this method sans step 7 will work even if you write your tests retroactively.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=qhFM5T6X-LM:Mul4pjQQO2c:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=qhFM5T6X-LM:Mul4pjQQO2c:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=qhFM5T6X-LM:Mul4pjQQO2c:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=qhFM5T6X-LM:Mul4pjQQO2c:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=qhFM5T6X-LM:Mul4pjQQO2c:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=qhFM5T6X-LM:Mul4pjQQO2c:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=qhFM5T6X-LM:Mul4pjQQO2c:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=qhFM5T6X-LM:Mul4pjQQO2c:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/qhFM5T6X-LM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/qhFM5T6X-LM/how-to-write-unit-test.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-BGyj_uL6vTU/UZPjmk84s6I/AAAAAAAADYM/gx8d-FjtnBM/s72-c/image_thumb%25255B7%25255D.png?imgmax=800" height="72" width="72" /><thr:total>4</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2013/05/how-to-write-unit-test.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-5771644014673970265</guid><pubDate>Wed, 24 Apr 2013 11:53:00 +0000</pubDate><atom:updated>2013-04-24T14:53:17.979+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Conference</category><category domain="http://www.blogger.com/atom/ns#">Mock objects</category><title>Sela Developer Practice 2013</title><description>&lt;p&gt;I’m speaking at &lt;a href="http://http://www.seladeveloperpractice.com/" target="_blank"&gt;SDP2013&lt;/a&gt; – two weeks from now.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-6FS7eMTmAZ8/UXfHqTN0XuI/AAAAAAAADV8/zkd1MaLYR8Q/s1600-h/email_confirmation_banner_107127EE6.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="email_confirmation_banner_107127EE" border="0" alt="email_confirmation_banner_107127EE" src="http://lh4.ggpht.com/-ibBMmwFvwIQ/UXfHrHp0H1I/AAAAAAAADWE/pi3PE-Vd5Yk/email_confirmation_banner_107127EE_t.png?imgmax=800" width="525" height="219" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I’ll be talking about mocking (surprise!) in “&lt;a href="http://www.seladeveloperpractice.com/sessions?selected=42" target="_blank"&gt;Battle of the Mocking Frameworks&lt;/a&gt;”.&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;So if you’re happen to come to the conference on May 7th – drop by and say hi.&lt;/p&gt;  &lt;p align="left"&gt;&amp;#160;&lt;/p&gt;  &lt;p align="left"&gt;Until then – happy coding…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_BJS8ekx4FM:xWgGIPBqhMc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_BJS8ekx4FM:xWgGIPBqhMc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=_BJS8ekx4FM:xWgGIPBqhMc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_BJS8ekx4FM:xWgGIPBqhMc:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_BJS8ekx4FM:xWgGIPBqhMc:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_BJS8ekx4FM:xWgGIPBqhMc:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_BJS8ekx4FM:xWgGIPBqhMc:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=_BJS8ekx4FM:xWgGIPBqhMc:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/_BJS8ekx4FM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/_BJS8ekx4FM/sela-developer-practice-2013.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-ibBMmwFvwIQ/UXfHrHp0H1I/AAAAAAAADWE/pi3PE-Vd5Yk/s72-c/email_confirmation_banner_107127EE_t.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2013/04/sela-developer-practice-2013.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-4868925758721278147</guid><pubDate>Tue, 26 Mar 2013 21:01:00 +0000</pubDate><atom:updated>2013-03-26T23:09:31.984+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Typemock</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Multi-threading</category><category domain="http://www.blogger.com/atom/ns#">Unit tests</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Unit testing multi threaded code–Timers</title><description>Writing unit tests for multi-threaded is not simple and could even be impossible for some scenarios – how could you test that an asynchronous method was not called?&lt;br /&gt;
Since most unit testing examples tend to be rather trivial I’ve decided to try and explain other more complex scenarios – preferably without using any &lt;em&gt;calculator&lt;/em&gt; examples.&lt;br /&gt;
&lt;h3&gt;
The “Timer” problem&lt;/h3&gt;
Consider the Timer class (or rather classes), .NET has several classes called &lt;em&gt;Timer, &lt;/em&gt;In this post I’m referring to &lt;em&gt;System.Threading.Timer &lt;/em&gt;and &lt;em&gt;System.Timers.Timer &lt;/em&gt;which is built on top of the former. &lt;br /&gt;
Both perform an action in another thread (using the Thread Pool) in intervals (not accurately – but I won’t go there in this post).&lt;br /&gt;
Consider the following class:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;public class ClassUnderTest
{
    private readonly Timer _timer;

    public ClassUnderTest()
    {
        _timer = new Timer(1000);
        _timer.Elapsed += PerformPeriodicAction;
        _timer.Start();
    }

    public ClassUnderTest(Timer timer)
    {
        _timer = timer;
        _timer.Elapsed += PerformPeriodicAction;
        _timer.Start();
    }

    private void PerformPeriodicAction(object sender, ElapsedEventArgs e)
    {
        // Perform very important task here!
    }
}&lt;/pre&gt;
Testing code that employs timers can be tricky. Unfortunately there’s a tendency to write a test that looks like this when timers are involved:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true; highlight: [7];"&gt;[Test]
public void TestJustWrong()
{
    var cut = new ClassUnderTest();

    // Make sure the timer executed at least once
    Thread.Sleep(5000);

    // Check that something happens
}&lt;/pre&gt;
&lt;h3&gt;
So what is the problem?&lt;/h3&gt;
Although it might seem like a valid unit test – in fact it’s a test that would fail from time to time - whenever the timer would happen to “tick” for more than the sleep period.&lt;br /&gt;
These kind of tests are known as the “the test that tends to fail” and usually “fixed” by running the build script another time.&lt;br /&gt;
You do not want a test that you cannot trust to fail only when a bug is introduced into your system.&lt;br /&gt;
Sadly I’ve seen this code – a lot! But no more - writing a good unit test for that class is simple, in fact there is more than one alternative that creates a simple, trustworthy unit test.&lt;br /&gt;
&lt;h3&gt;
Solution 1 – invoke the handler instead&lt;/h3&gt;
Instead of waiting for the time to execute why not call the method that handles the timer’s event?&lt;br /&gt;
First we need to make sure that the method we’re going to call is internal or public. I personally don’t like changing my production code that much in order to make the code “testable” but it’s &lt;u&gt;a&lt;/u&gt; way.&lt;br /&gt;
Now all we have to do is call the method thus making sure that execution happens exactly when we want it – and a unit test is born:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;[Test]
public void TestByMethod()
{
    var cut = new ClassUnderTestForTestability();

    // Let's call the method
    cut.PerformPeriodicAction(this, null);

    // Check that something happens
}&lt;/pre&gt;
&lt;h3&gt;
Solution 2 – invoke the timer at your leisure&lt;/h3&gt;
Most modern Isolation (a.k.a Mocking) frameworks have the ability to invoke events as well as create fake objects.&lt;br /&gt;
We’re going to create a &lt;em&gt;fake&lt;/em&gt; timer and invoke the elapsed event. Using simple constructor injection we’ll pass the timer to our class under test prior to invocation and we have a more robust unit test:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;[Test]
public void TestWithFakeTimer()
{
    var fakeTimer = Isolate.Fake.Instance&amp;lt;Timer&amp;gt;();

    var cut = new ClassUnderTest(fakeTimer);

    // Let's invoke the event
    Isolate.Invoke.Event(() =&amp;gt; fakeTimer.Elapsed += null, this, null);

    // Check that something happens
}&lt;/pre&gt;
I’m using Typemock Isolator but you can use the isolation framework of your choice The syntax is strange but once you’ll get used to it, it’s simple.&lt;br /&gt;
The added value is that we’re causing the real flow of our program to execute – in a synchronized and timely manner.&lt;br /&gt;
&lt;h3&gt;
Conclusion&lt;/h3&gt;
Although there are many ways to test code that uses timers I find myself employing the two above most of the time.&lt;br /&gt;
I plan to write a few more posts on the other pitfall of testing multi-threaded code (and how to avoid them) but until then – &lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: center;"&gt;
happy coding…&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=jWSo44qe4Ws:GGt2uSZvY3E:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=jWSo44qe4Ws:GGt2uSZvY3E:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=jWSo44qe4Ws:GGt2uSZvY3E:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=jWSo44qe4Ws:GGt2uSZvY3E:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=jWSo44qe4Ws:GGt2uSZvY3E:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=jWSo44qe4Ws:GGt2uSZvY3E:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=jWSo44qe4Ws:GGt2uSZvY3E:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=jWSo44qe4Ws:GGt2uSZvY3E:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/jWSo44qe4Ws" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/jWSo44qe4Ws/unit-testing-multi-threaded-codetimers.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2013/03/unit-testing-multi-threaded-codetimers.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-692440617823231803</guid><pubDate>Thu, 28 Feb 2013 22:17:00 +0000</pubDate><atom:updated>2013-03-01T00:19:39.910+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Scrum</category><category domain="http://www.blogger.com/atom/ns#">Agile</category><title>Spreading the agile joy</title><description>&lt;p&gt;I finally hit critical mass! &lt;/p&gt;  &lt;p&gt;I got a request from one of the project managers (not my project) to do a quick “introduction to Scrum” session.&lt;/p&gt;  &lt;p&gt;It took quite a while – more than two years (&lt;a href="http://blog.drorhelper.com/2012/12/agile-might-save-your-job.html" target="_blank"&gt;and a downsizing&lt;/a&gt;) in order for other teams to notice that we have a different way to do things.&lt;/p&gt;  &lt;p&gt;I did a very successful session – mainly because the audience “felt the pain”; it was fun explaining about Scrum with examples on how they can utilize it the very next day.&lt;/p&gt;  &lt;p&gt;Since I’m not in daily contact with that team – I had no idea how well they implemented Scrum. Until yesterday when I walked on a meeting that had a task board on a big screen – so I guess it went well.&lt;/p&gt;  &lt;p&gt;When I was first asked to do this session I wanted to create a simple and short presentation that can be re-used – And here it is:&lt;/p&gt; &lt;iframe style="margin-bottom: 5px; border-top: #ccc 1px solid; border-right: #ccc 1px solid; border-bottom: #ccc 0px solid; border-left: #ccc 1px solid" height="356" marginheight="0" src="http://www.slideshare.net/slideshow/embed_code/16843413" frameborder="0" width="427" marginwidth="0" scrolling="no" mozallowfullscreen="mozallowfullscreen" webkitallowfullscreen="webkitallowfullscreen" allowfullscreen="allowfullscreen"&gt; &lt;/iframe&gt;  &lt;div style="margin-bottom: 5px"&gt;&lt;strong&gt;&lt;a title="Introduction to scrum" href="http://www.slideshare.net/dhelper/introduction-to-scrum-16843413" target="_blank"&gt;Introduction to scrum&lt;/a&gt; &lt;/strong&gt;from &lt;strong&gt;&lt;a href="http://www.slideshare.net/dhelper" target="_blank"&gt;Dror Helper&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;It’s simple and short so I use it as a “background” for a discussion on how they are going to use Scrum in their work – today.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I guess I’m doing something right since I have another session next week!&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;And if you have an agile success story – please share…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=tt1RuMx1dSw:qwjXZt2XuBQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=tt1RuMx1dSw:qwjXZt2XuBQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=tt1RuMx1dSw:qwjXZt2XuBQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=tt1RuMx1dSw:qwjXZt2XuBQ:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=tt1RuMx1dSw:qwjXZt2XuBQ:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=tt1RuMx1dSw:qwjXZt2XuBQ:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=tt1RuMx1dSw:qwjXZt2XuBQ:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=tt1RuMx1dSw:qwjXZt2XuBQ:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/tt1RuMx1dSw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/tt1RuMx1dSw/spreading-agile-joy.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>1</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2013/03/spreading-agile-joy.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-8686536330439471351</guid><pubDate>Sun, 03 Feb 2013 18:23:00 +0000</pubDate><atom:updated>2013-02-14T00:05:10.674+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Book review</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Things I learnt reading C# specifications (#1)</title><description>&lt;a href="http://lh3.ggpht.com/-wPZO2oDXdZ0/UQ6rJ7j515I/AAAAAAAADRg/rznhfD_YUl4/s1600-h/9780321741769%25255B5%25255D.jpg"&gt;&lt;img align="left" alt="9780321741769" border="0" height="239" src="http://lh6.ggpht.com/-TUUuGM1-ffM/UQ6rLAYPiWI/AAAAAAAADRo/I31cr9CpyQs/9780321741769_thumb%25255B3%25255D.jpg?imgmax=800" style="background-image: none; border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: inline; float: left; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="9780321741769" width="138" /&gt;&lt;/a&gt;After reading Jon Skeet’s excellent &lt;a href="http://www.manning.com/skeet/" target="_blank"&gt;C# in Depth&lt;/a&gt; - &lt;a href="http://blog.drorhelper.com/2011/01/book-review-c-in-depth-second-edition.html" target="_blank"&gt;again&lt;/a&gt; (3rd edition - to be published soon) I’ve decide to try and actually read the &lt;a href="http://msdn.microsoft.com/en-us/library/ms228593.aspx" target="_blank"&gt;C# language specification&lt;/a&gt;…&lt;br /&gt;
Being a sensible kind of guy I’ve decided to purchase the &lt;a href="http://www.amazon.com/Programming-Language-Covering-Microsoft-Development/dp/0321741765" target="_blank"&gt;annotated version&lt;/a&gt; which only cover topics up to .NET 4 – but has priceless comments from several C# gurus.&lt;br /&gt;
After I’ve read a few pages I was amazed to learn that a few things I knew to be true were completely wrong and so I’ve decided to write a list of new things I’ve learnt while reading this book.&lt;br /&gt;
Below you’ll find a short list of new things I learnt from reading the 1st chapter:&lt;br /&gt;
&lt;h3&gt;
Not all value types are saved on the stack&lt;/h3&gt;
Many developers believe that reference types are stored on the heap while value types are &lt;em&gt;always&lt;/em&gt;&amp;nbsp; stored on the stack – this is not entirely true.&lt;br /&gt;
First it’s more of an implementation detail of the actual runtime and not a language requirement but more importantly it’s not possible – consider a class (a.k.a reference type) which has a integer member (a.k.a value type),&amp;nbsp; the class is stored on the heap and so are it’s members including the value type since its data is copied “by-value”.&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;class MyClass
{
    // stored in heap
    int a = 5;  
}&lt;/pre&gt;
&lt;br /&gt;
For more information &lt;a href="http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx" target="_blank"&gt;read Eric Lippert’s post on the subject&lt;/a&gt; – he should know.&lt;br /&gt;
&lt;h3&gt;
What the hell is “&lt;em&gt;protected internal”&lt;/em&gt; &lt;/h3&gt;
If you’ve written more than one class you’ve probably used public/internal and protected access modifier:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;class MyClass
{
    // Can only be accessed by MyClass
    private object a;

    // Accessible by MyClass and classes that derive from it
    protected object b;

    // Accessible by this assembly
    internal object c;

    // Accessible by everyone 
    public object d;
}&lt;/pre&gt;
&lt;br /&gt;
So what does&amp;nbsp;&lt;em&gt;protected internal &lt;/em&gt;means?&lt;br /&gt;
&lt;br /&gt;
Some believe that members marked as “protected internal” are only accessible for classes that derive from MyClass &lt;strong&gt;&lt;u&gt;AND&lt;/u&gt;&lt;/strong&gt; are defined at the same assembly in fact it means that classes that derive from MyClass &lt;strong&gt;&lt;u&gt;OR&lt;/u&gt;&lt;/strong&gt; are defined on the same assembly as my class can access that member. So it’s just like using internal and protected at the same time – confused yet?&lt;br /&gt;
&lt;h3&gt;
Use only immutable types as a readonly fields&lt;/h3&gt;
How many times have you marked a field &lt;a href="http://msdn.microsoft.com/en-us/library/acdd6hb7.aspx" target="_blank"&gt;readonly&lt;/a&gt;? &lt;br /&gt;
&lt;br /&gt;
You probably did it to make sure that a field cannot change after initialization – think again:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;class Person
{
    static readonly Person Me = new Person("Dror", "Helper");
}

public void UpdateFail()
{
    // Compilation error!
    Person.Me = new Person("Kaiser", "Soze");
}

public void JustBecauseYouCan()
{
    // This would work!
    Person.Me.First = "Kaiser";
    Person.Me.Last = "Suze";
}   &lt;/pre&gt;
&lt;br /&gt;
That’s right, while you cannot replace the the readonly field – you can update the heck out of it.&lt;br /&gt;
&lt;br /&gt;
Of course there’s more, I choose these examples because they helped me understand C# better.&lt;br /&gt;
&lt;br /&gt;
I hope to add more such insights as I continue reading the book – so stay tuned…&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=OUSbon0fosg:qQCauvk-X0c:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=OUSbon0fosg:qQCauvk-X0c:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=OUSbon0fosg:qQCauvk-X0c:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=OUSbon0fosg:qQCauvk-X0c:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=OUSbon0fosg:qQCauvk-X0c:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=OUSbon0fosg:qQCauvk-X0c:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=OUSbon0fosg:qQCauvk-X0c:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=OUSbon0fosg:qQCauvk-X0c:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/OUSbon0fosg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/OUSbon0fosg/things-i-learnt-reading-c.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-TUUuGM1-ffM/UQ6rLAYPiWI/AAAAAAAADRo/I31cr9CpyQs/s72-c/9780321741769_thumb%25255B3%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>20</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2013/02/things-i-learnt-reading-c.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-4048482611909388544</guid><pubDate>Fri, 11 Jan 2013 14:04:00 +0000</pubDate><atom:updated>2013-01-11T16:07:31.735+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">DSL</category><category domain="http://www.blogger.com/atom/ns#">Tips and Tricks</category><title>Fluent interfaces in C# – Generics</title><description>If you haven’t read the previous posts on the subject of fluent interfaces using C# – I suggest you do so now – I’ll wait…&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://blog.drorhelper.com/2012/07/fluent-interfaces-in-cintroduction.html"&gt;Introduction&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.drorhelper.com/2012/07/fluent-interfaces-in-c-extension.html"&gt;Extension Methods&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;&lt;a href="http://blog.drorhelper.com/2012/09/fluent-interfaces-in-c-method-chaining.html" target="_blank"&gt;Method Chaining&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;
Great! now you’re all ready for the forth post on the subject of using &lt;a href="http://msdn.microsoft.com/en-us/library/512aeb7t.aspx" target="_blank"&gt;Generics&lt;/a&gt;:&lt;br /&gt;
&lt;blockquote&gt;
Generics were added to version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.&lt;br /&gt;
[&lt;a href="http://msdn.microsoft.com/en-us/library/512aeb7t.aspx" target="_blank"&gt;Generics (C# Programming Guide)&lt;/a&gt;]&lt;/blockquote&gt;
&lt;h3&gt;
Using generics tor return specific type&lt;/h3&gt;
&lt;span style="color: #555555;"&gt;If you’ve been programming in C# you’re probably seen generics in the past– either a type safe collection (&lt;em&gt;i.e. List&amp;lt;T&amp;gt;&lt;/em&gt;) or written your own generic class/method. &lt;/span&gt;&lt;br /&gt;
Using Generics in your API is just like saying “of type X” as part of the interface – for example “create a fake/mock &lt;em&gt;of type ILogger&lt;/em&gt;”:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;// Typemock Isolator
var fakeLogger = Isolate.Fake.Instance&amp;lt;ILogger&amp;gt;();

// Telerik's JustMock
var fakeLogger = Mock.Create&amp;lt;ILogger&amp;gt;();

// NSubstitute 
var fakeLogger = Substitute.For&amp;lt;ILogger&amp;gt;();&lt;/pre&gt;
&lt;br /&gt;
Reading the code above you’ll notice something - even if you are not familiar with the topic of mocking frameworks you can still understand what each line does.&lt;br /&gt;
&lt;br /&gt;
The reason that this API is used in mocking frameworks is that they all have something in common – they need to create a new instance of type X -&amp;nbsp; and so they get the type parameter as part of the method call a.k.a generics.&lt;br /&gt;
&lt;h3&gt;
Using Generics to configure action(s) on specific type&lt;/h3&gt;
If you’re familiar with .NET IoC you might recognize the following API:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true; highlight: [2];"&gt;Kernel.Register(
   Component.For&amp;lt;ICustomer&amp;gt;()
       .Named("customer")
       .ImplementedBy&amp;lt;CustomerImpl&amp;gt;()
       .Activator&amp;lt;MyCustomerActivator&amp;gt;()
   );&lt;/pre&gt;
&lt;br /&gt;
In this example we use generics to configure a component and bind implementation to specific type and use another concrete type to create that instance.&lt;br /&gt;
&lt;br /&gt;
Another example which I use in my project is binding message types to handling method. We have a TCP client (non WCF – what a shame) that returns messages from a single event. Each message type should be handled by a different method (logic).&lt;br /&gt;
&lt;br /&gt;
Using generics and &lt;a href="http://blog.drorhelper.com/2012/09/fluent-interfaces-in-c-method-chaining.html" target="_blank"&gt;method chaining&lt;/a&gt; got us the following solution:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;var repo = new MessageHandlerContainer();
repo.Register&amp;lt;ConcreteMessage1&amp;gt;().With(SendEmailNotification)
    .Register&amp;lt;AnotherMessage&amp;gt;().With(SoundAlarm);

// when a message arrive
var newMessage = GetNextMessage();

container.ProcessMessage(newMessage);&lt;/pre&gt;
&lt;br /&gt;
Easy to understand what each the code does – without reading the actual implementation.&lt;br /&gt;
&lt;br /&gt;
But just in case – here you go:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;public class MessageHandlerContainer
{
    private readonly Dictionary&amp;lt;Type, IMessageHandler&amp;gt; _messageHandlers =
        new Dictionary&amp;lt;Type, IMessageHandler&amp;gt;();

    public void ProcessMessage(IMessage message)
    {
        _messageHandlers[message.GetType()].Run();
    }

    public MessageHandler&amp;lt;T&amp;gt; Register&amp;lt;T&amp;gt;() where T : IMessage
    {
        var messageHandler = new MessageHandler&amp;lt;T&amp;gt;();

        _messageHandlers.Add(typeof(T), messageHandler);

        return messageHandler;
    }
}&lt;/pre&gt;
&lt;br /&gt;
The trick in inside the &lt;em&gt;MessageHandler&lt;/em&gt; class which adds an action and return the parent container so that you may register yet another message:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;public class MessageHandler&amp;lt;T&amp;gt; : IMessageHandler
{
    private readonly MessageHandlerContainer _parent;
    private Action&amp;lt;IMessage&amp;gt; _actionToRun;

    public MessageHandler(MessageHandlerContainer parent)
    {
        _parent = parent;
    }

    public MessageHandlerContainer WithHandler(Action&amp;lt;IMessage&amp;gt; actionToRun)
    {
        _actionToRun = actionToRun;

        return _parent;
    }

    public void Run()
    {
        _actionToRun();
    }
}&lt;/pre&gt;
&lt;br /&gt;
It’s not the simplest code to follow – but when you see the usage example (scroll up) you’ll notice it makes a very readable API. Now it’s very simple to add another message handler to the logic without risking spaghetti code.&lt;br /&gt;
&lt;h3&gt;
Conclusion&lt;/h3&gt;
Generics are very powerful and can be used to in your fluent API to create and register specific types.&lt;br /&gt;
&lt;br /&gt;
There are other uses for generics other than these two but you’ll have to wait for a different blog post – or experiment and find out by yourself.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N19PY5vtpmU:LIdlc0Anf0Q:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N19PY5vtpmU:LIdlc0Anf0Q:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=N19PY5vtpmU:LIdlc0Anf0Q:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N19PY5vtpmU:LIdlc0Anf0Q:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N19PY5vtpmU:LIdlc0Anf0Q:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N19PY5vtpmU:LIdlc0Anf0Q:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N19PY5vtpmU:LIdlc0Anf0Q:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=N19PY5vtpmU:LIdlc0Anf0Q:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/N19PY5vtpmU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/N19PY5vtpmU/fluent-interfaces-in-c-generics.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2013/01/fluent-interfaces-in-c-generics.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-5140939591031542729</guid><pubDate>Sat, 29 Dec 2012 20:20:00 +0000</pubDate><atom:updated>2012-12-29T22:20:22.119+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Unit tests</category><category domain="http://www.blogger.com/atom/ns#">Agile</category><category domain="http://www.blogger.com/atom/ns#">XUnit</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><category domain="http://www.blogger.com/atom/ns#">TDD</category><title>nCrunch review – TDD on steroids</title><description>&lt;p&gt;Any project that uses unit tests gets to the stage where running all of the tests takes a lot of time. Even if you manage to keep your test suite in a manageable size From time to time a developer would “forget” to run all the tests before commit and break the build.&lt;/p&gt;  &lt;p&gt;One solution for these problems is to have an automatic test runner/continuous testing tool that runs the tests automatically.&lt;/p&gt;  &lt;p&gt;The idea of continuous testing has it’s roots in the Java world. &lt;a href="http://junitmax.com/" target="_blank"&gt;JUnitMax&lt;/a&gt; which was create by &lt;a href="http://en.wikipedia.org/wiki/Kent_Beck" target="_blank"&gt;Kent Beck&lt;/a&gt;, runs &lt;u&gt;all of the tests&lt;/u&gt; on each save. The order of the tests is determined by risk – the first tests run were the ones that failed on the previous run. Although this solution does not solve long run times it does make sure that your code is constantly being verified.&lt;/p&gt;  &lt;p&gt;In the .NET space there are several such tools one of which shine above the rest -&amp;#160; &lt;a href="http://www.ncrunch.net/" target="_blank"&gt;nCrunch&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;Getting started&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://www.ncrunch.net/" target="_blank"&gt;nCrunch&lt;/a&gt; is code coverage, test runner and performance analyzer all in one. Using it is simple – all you need to do is to choose to enable it and after filling a few options you’re ready to go.&lt;/p&gt;  &lt;p&gt;You can choose how many CPU cores to dedicate to running of tests and whether or not you wish to run your tests in parallel. Another option is to run the test manually – in case you “only” wish to use nCrunch as a test runner.&lt;/p&gt;  &lt;h3&gt;Running nCrunch&lt;/h3&gt;  &lt;p&gt;After this short configuration nCrunch would automatically discover all of the unit test in your solution and try to run them. So far I’ve used it with &lt;a href="http://nunit.,org" target="_blank"&gt;NUnit&lt;/a&gt;, and &lt;a href="http://xunit.codeplex.com/" target="_blank"&gt;XUnit&lt;/a&gt; and it worked perfectly. On each save the code is compiled, and tests are run . &lt;a href="http://www.ncrunch.net/" target="_blank"&gt;nCrunch&lt;/a&gt; can be configured to run only the tests affected by the last code change. I’ve used this option thus avoiding a long test run with each change I make. &lt;/p&gt;  &lt;p&gt;Another plus is the continuous code coverage results, each line of code written is immediately marked as covered/not covered by unit tests and if that unit test(s) fail – it is also marked in red.&lt;/p&gt;  &lt;p&gt;When a test fails nCrunch provide full exception information and an arsenal of debugging options to choose from – from simple “run in debug” to “Break into the first failing covering test at this line”.&lt;/p&gt;  &lt;h3&gt;Supercharge your unit tests&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://www.ncrunch.net/" target="_blank"&gt;nCrunch&lt;/a&gt; is one of these tools I’ve been looking for without actually realizing it. Using it makes TDD (&lt;a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank"&gt;Test Drive Development&lt;/a&gt;) easy to follow. If you want an excellent example – take a look at this video (YouTube):&lt;/p&gt; &lt;iframe height="315" src="http://www.youtube.com/embed/mlZJzbSe-Fc" frameborder="0" width="560" allowfullscreen="allowfullscreen"&gt;&lt;/iframe&gt;  &lt;p&gt;Around 1:10 it really gets interesting when the code is written and the tests are executed side by side&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;&lt;a href="http://www.ncrunch.net/" target="_blank"&gt;nCrunch&lt;/a&gt; is not the only continuous testing tool out there but it’s certainly the most mature one – with easy configuration and many useful features it’s the unit testing productivity tool that I was look for.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=bKMsS15LvJk:fSbgUnIH9GY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=bKMsS15LvJk:fSbgUnIH9GY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=bKMsS15LvJk:fSbgUnIH9GY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=bKMsS15LvJk:fSbgUnIH9GY:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=bKMsS15LvJk:fSbgUnIH9GY:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=bKMsS15LvJk:fSbgUnIH9GY:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=bKMsS15LvJk:fSbgUnIH9GY:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=bKMsS15LvJk:fSbgUnIH9GY:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/bKMsS15LvJk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/bKMsS15LvJk/ncrunch-review-tdd-on-steroids.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/mlZJzbSe-Fc/default.jpg" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/12/ncrunch-review-tdd-on-steroids.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-5639926767415603508</guid><pubDate>Wed, 19 Dec 2012 19:45:00 +0000</pubDate><atom:updated>2012-12-19T21:48:48.072+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Tips and Tricks</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Serialization and events in C#</title><description>Today I had an interesting problem:&lt;br /&gt;
I was trying to serialize and object using &lt;a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.71).aspx" target="_blank"&gt;BinaryFormatter&lt;/a&gt; but it kept on failing because some class was not &lt;em&gt;Serializable. &lt;/em&gt;I’ve double and triple checked my class and all of it’s inner properties and verified that indeed they were marked correctly.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Problem&lt;/h3&gt;
Looking closer at the exception I’ve noticed something – the problematic class that was causing me grief was not part of the class. In fact the problematic class was signed to an event of the class I was trying to serialize. SO I had the following code:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;var myObj = new SerializableClass();

var notSerializble = new NotSerializableClass(myObj);

IFormatter formatter = new BinaryFormatter();

using (Stream stream = new MemoryStream())
{
    formatter.Serialize(stream, myObj);
    
    ...
}&lt;/pre&gt;
And the the class – &lt;em&gt;NotSerializableClass&lt;/em&gt; was something like this:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;public class NotSerializableClass
{
    private SerializableClass myObj;

    public NotSerializableClass(SerializableClass myObj)
    {
        myObj.someEvent += HandleSomeEvent;

        ...&lt;/pre&gt;
&lt;br /&gt;
And so the fact that it was attached to the event from my perfectly serializable class caused the problem – the &lt;em&gt;BinaryFormatter&lt;/em&gt; was trying to serialize it as well!&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
The Solution&lt;/h3&gt;
Once I understood the problem the solution was simple – I’ve marked the event as &lt;em&gt;field:NonSerialized &lt;/em&gt;and the problem was solved:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true; highlight: [4];"&gt;[Serializable]
public class SerializableClass
{
    [field:NonSerialized]
    public event EventHandler someEvent;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Simple – as long as you know what to look for.&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=Yw6aya4OGHI:3xx0qwWm360:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=Yw6aya4OGHI:3xx0qwWm360:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=Yw6aya4OGHI:3xx0qwWm360:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=Yw6aya4OGHI:3xx0qwWm360:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=Yw6aya4OGHI:3xx0qwWm360:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=Yw6aya4OGHI:3xx0qwWm360:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=Yw6aya4OGHI:3xx0qwWm360:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=Yw6aya4OGHI:3xx0qwWm360:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/Yw6aya4OGHI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/Yw6aya4OGHI/serialization-and-events-in-c.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>1</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/12/serialization-and-events-in-c.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-4232104239434294331</guid><pubDate>Tue, 04 Dec 2012 20:56:00 +0000</pubDate><atom:updated>2012-12-13T18:49:41.413+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Thoughts</category><category domain="http://www.blogger.com/atom/ns#">Agile</category><title>Agile might save your job</title><description>&lt;p&gt;Imagine this: you have two employees, both are hardworking and bright.&lt;/p&gt;  &lt;p&gt;You have complete knowledge of what the first employee does, which task he’s working on and its deadline while you have absolutely no idea what the other employee does. While the first employee shows progress every few weeks with new release, the other commits to quarterly plan. The first employee give you daily progress report and notify in advance when he’s going to miss a deadline or a feature.&lt;/p&gt;  &lt;p&gt;Now imagine that you must fire one of your employees – which would you choose?&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-pPDJBtB63c0/UMoHHLBm2-I/AAAAAAAADQo/R23L-YuVQ4M/s1600-h/those_76afda_235997%25255B5%25255D.jpg"&gt;&lt;img title="those_76afda_235997" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="those_76afda_235997" src="http://lh3.ggpht.com/-Q1xTwIsV_1o/UMoHInUCr3I/AAAAAAAADQw/uj6vDfvCfaM/those_76afda_235997_thumb%25255B3%25255D.jpg?imgmax=800" width="554" height="470" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Truth be told most managers prefer employees that shows progress and keeps them well informed. &lt;/p&gt;  &lt;p&gt;So how can one protect his job – simply by making sure that his manager and manager’s manger are aware of the state of the tasks under his responsibility and if a deadline is going to be missed – be notify about it as soon as possible – because no manager loves surprises as far as his project is concerned.&lt;/p&gt;  &lt;p&gt;The thing is that all of this and more is part of several agile methodologies. It does not matter if you practice SCRUM, Kanban or Lean notifying your manager and tracking progress is an integral part of the process used.&lt;/p&gt;  &lt;p&gt;It also helps that following agile methodologies would guarantee that progress is always seen – one iteration after the other.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;And the moral of this story (as you might have guessed) is that the Agile &lt;u&gt;team&lt;/u&gt; was not fired and they get to do at least one more iteration.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Just a thought…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=knpmZvMlsao:AiaMxAQJ0PQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=knpmZvMlsao:AiaMxAQJ0PQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=knpmZvMlsao:AiaMxAQJ0PQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=knpmZvMlsao:AiaMxAQJ0PQ:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=knpmZvMlsao:AiaMxAQJ0PQ:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=knpmZvMlsao:AiaMxAQJ0PQ:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=knpmZvMlsao:AiaMxAQJ0PQ:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=knpmZvMlsao:AiaMxAQJ0PQ:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/knpmZvMlsao" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/knpmZvMlsao/agile-might-save-your-job.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-Q1xTwIsV_1o/UMoHInUCr3I/AAAAAAAADQw/uj6vDfvCfaM/s72-c/those_76afda_235997_thumb%25255B3%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/12/agile-might-save-your-job.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-3626091516807405047</guid><pubDate>Mon, 05 Nov 2012 20:05:00 +0000</pubDate><atom:updated>2012-11-05T22:07:23.002+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">DSL</category><category domain="http://www.blogger.com/atom/ns#">Unit tests</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><title>Making string based method strongly typed</title><description>I can't believe they implemented this way – was the first thought on my mind…&lt;br /&gt;
The method in question is part of the new(er) &lt;a href="http://nunit.org/" target="_blank"&gt;NUnit&lt;/a&gt;’s constraint based model specifically the one used to assert a property value.&lt;br /&gt;
Before we dive deeper a few words on the constraint based model:&lt;br /&gt;
&lt;h3&gt;
constraint based model&lt;/h3&gt;
Most unit testing framework have similar way of writing assertions – if you’ve wrote a test in the past it probably looked something like this:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;Assert.AreEqual(4, 2 + 2);&lt;/pre&gt;
&lt;br /&gt;
NUnit has another API for assertion which is more to my liking and using it for the above assertion would look like this:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;Assert.That(2 + 2, Is.EqualTo(4));&lt;/pre&gt;
&lt;br /&gt;
Using this API I can write pretty cool things:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;// Simple values
Assert.That(someInt, Is.Negative);
Assert.That(someString, Is.Not.Null.And.Not.Empty);
Assert.That(someObject, Is.Null);

// Collections
Assert.That(someList, Is.Not.Empty);
Assert.That(intArray, Is.All.GreaterThan(10); // All intergers in array are bigger than 10
Assert.That(array, Has.Exactly(1).GreaterThan(3)); // Exactly one item bigger than 3
Assert.That(someEnumerable, Is.All.Property("MyProp").EqualTo(25));&lt;/pre&gt;
&lt;br /&gt;
I have an issue with the last line. I don’t like writing string based code – mainly it tend to break on trivial refactoring and writing assertion that could break &lt;u&gt;on runtime&lt;/u&gt;&amp;nbsp; just because someone has changed the property name seems just wrong.&lt;br /&gt;
I have as compiler and I’m not afraid to use it – the least you can do is give me compile time errors!&lt;br /&gt;
&lt;h3&gt;
The solution&lt;/h3&gt;
I’ve wanted to add another method to &lt;em&gt;Is.All&lt;/em&gt; and so I’ve used one of fluent API writers friend – the &lt;a href="http://blog.drorhelper.com/2012/07/fluent-interfaces-in-c-extension.html" target="_blank"&gt;extension method&lt;/a&gt; and I got the following API:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;Assert.That(someEnumerable, Is.All.Property&amp;lt;MyType&amp;gt;(m =&amp;gt; m.MyProp).EqualTo(25));&lt;/pre&gt;
&lt;br /&gt;
Using black lambda tricks I was able to create a strongly typed API that receive a property.&lt;br /&gt;
&lt;br /&gt;
After I had my entry point sorted out all I needed to do is write (and copy shamelessly) some cool Expression based code to get the property name and it works!&lt;br /&gt;
&lt;br /&gt;
Here is the full code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;public static ResolvableConstraintExpression Property&amp;lt;T&amp;gt;(
  this NUnit.Framework.Constraints.ConstraintExpression expression,   Expression&amp;lt;func&amp;lt;T, object&amp;gt;&amp;gt; lambda)
{
    MemberExpression memberExpression = null;
    switch (lambda.Body.NodeType)
    {
        case ExpressionType.Convert:
            // lambda is obj =&amp;gt; Convert(obj.Prop) - the operand of conversion is our member expression
            var unary = lambda.Body as UnaryExpression;
            if (unary == null)
            {
                Assert.Fail("Cannot parse expression");
            }
            memberExpression = unary.Operand as MemberExpression;
            break;

        case ExpressionType.MemberAccess:
            // lambda is (obj =&amp;gt; obj.Prop) - the body is the member expression
            memberExpression = lambda.Body as MemberExpression;
            break;

        default:
            Assert.Fail("Cannot parse expression");
            break;
    }

    if (memberExpression == null ||  string.IsNullOrEmpty(memberExpression.Member.Name))
    {
        Assert.Fail("Labda body is not MemberExpression - use only properties");
    }

    var propertyName = memberExpression.Member.Name;

    return expression.Property(propertyName);
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
I hope it would help at least one reader who cannot accept a string based API.&lt;br /&gt;
&lt;br /&gt;
Happy Coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=fN1GDww5wT0:QVv2WcrtPuw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=fN1GDww5wT0:QVv2WcrtPuw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=fN1GDww5wT0:QVv2WcrtPuw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=fN1GDww5wT0:QVv2WcrtPuw:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=fN1GDww5wT0:QVv2WcrtPuw:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=fN1GDww5wT0:QVv2WcrtPuw:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=fN1GDww5wT0:QVv2WcrtPuw:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=fN1GDww5wT0:QVv2WcrtPuw:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/fN1GDww5wT0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/fN1GDww5wT0/making-string-based-method-strongly.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>1</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/11/making-string-based-method-strongly.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-3147048349292617638</guid><pubDate>Fri, 02 Nov 2012 08:23:00 +0000</pubDate><atom:updated>2012-11-02T10:25:34.799+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">BDD</category><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">XUnit</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Unit level BDD using SubSpec</title><description>In the &lt;a href="http://en.wikipedia.org/wiki/Behavior-driven_development" target="_blank"&gt;BDD&lt;/a&gt; (Behavior Drive Development) there are two groups of frameworks. The first group contains tools such as &lt;a href="http://www.specflow.org/specflownew/" target="_blank"&gt;SpecFlow&lt;/a&gt; and &lt;a href="http://nbehave.org/" target="_blank"&gt;nBehave&lt;/a&gt; to name a few all of which uses (at least) two files – one contains the scenarios/behaviors in plain English (more or less) and the other the code that makes it all happen.&lt;br /&gt;
I want to discuss the other type – the one that contains “unit level” BDD. Unlike the first group the behaviors are written inside the code where only us developers can read them – you can always try to convince your product/marketing manager to read your code but it tends to go downhill from there…&lt;br /&gt;
The reason I’m telling you this is that this week I discovered a very interesting tool – &lt;a href="https://bitbucket.org/johannesrudolph/subspec/wiki/Home" target="_blank"&gt;SubSpec&lt;/a&gt;. It uses extension methods on &lt;em&gt;System.String, lambdas &lt;/em&gt;and some nifty &lt;a href="http://xunit.codeplex.com/" target="_blank"&gt;XUnit&lt;/a&gt; tricks to make BDD accessible and easy to use.&lt;br /&gt;
&lt;h3&gt;
Getting started&lt;/h3&gt;
Using &lt;a href="https://bitbucket.org/johannesrudolph/subspec/wiki/Home" target="_blank"&gt;SubSpec&lt;/a&gt; is simple. After creating a new project add a reference to SubSpec using &lt;a href="http://nuget.codeplex.com/" target="_blank"&gt;NuGet&lt;/a&gt;:&lt;br /&gt;
&lt;a href="http://lh4.ggpht.com/-MM7RWF9Tk6A/UJOCp9udKFI/AAAAAAAADPQ/h3SY-fQxAFQ/s1600-h/image%25255B3%25255D.png"&gt;&lt;img alt="image" border="0" height="604" src="http://lh3.ggpht.com/-MvPpkHK_gI4/UJOCr_BMRNI/AAAAAAAADPY/XsXdEHq4HEc/image_thumb%25255B1%25255D.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="904" /&gt;&lt;/a&gt;&lt;br /&gt;
Adding SubSpec via NuGet will also make sure that you have the required dependencies - XUnit and XUnit.Extensions:&lt;br /&gt;
&lt;a href="http://lh4.ggpht.com/-EIxLPJV7XiA/UJOCs0GhzAI/AAAAAAAADPc/7U3Oxy-lTN4/s1600-h/image%25255B10%25255D.png"&gt;&lt;img alt="image" border="0" height="406" src="http://lh4.ggpht.com/-9eSt_2N3vbM/UJOC88IFTTI/AAAAAAAADPo/gq6X630eTDs/image_thumb%25255B6%25255D.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="354" /&gt;&lt;/a&gt;&lt;br /&gt;
And you’re ready to go…&lt;br /&gt;
&lt;h3&gt;
Writing the first behavior&lt;/h3&gt;
Like any other test SubSpec test is divided into three parts – Context, Action and Verifications.&lt;br /&gt;
The entry point to all three is a &lt;em&gt;string&lt;/em&gt;. &lt;br /&gt;
Writing a new test is done by following these four steps:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Add &lt;em&gt;Specification&lt;/em&gt; attribute to the test method &lt;/li&gt;
&lt;li&gt;Use &lt;em&gt;Context&lt;/em&gt; to initialize the class under test &lt;/li&gt;
&lt;li&gt;Use &lt;em&gt;Do&lt;/em&gt; to perform an action &lt;/li&gt;
&lt;li&gt;Use &lt;em&gt;Assert&lt;/em&gt; to test for success/failure &lt;/li&gt;
&lt;/ol&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;[Specification]
public void StoreWithOneProduct()
{
    const string productId = "prod-1";

    var store = default(Store);

    "Given a new store".Context(() =&amp;gt; store = new Store());

    "with one product".Do(() =&amp;gt; store.AddProduct(productId));

    "The store has that product".Assert(() =&amp;gt; Assert.True(store.HasProduct(productId)));
}&lt;/pre&gt;
&lt;br /&gt;
Because &lt;a href="https://bitbucket.org/johannesrudolph/subspec/wiki/Home" target="_blank"&gt;SubSpec&lt;/a&gt; is built on top of &lt;u&gt;XUnit any runner that is able to run XUnit tests can run these specifications&lt;/u&gt;. I’ve used the freely available &lt;a href="http://xunit.codeplex.com/" target="_blank"&gt;XUnit&lt;/a&gt; Runner for VS2012 and got this results:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/-9SacDvM6BuE/UJOC9tPzz0I/AAAAAAAADPw/Gj6GPpi82ew/s1600-h/image%25255B18%25255D.png"&gt;&lt;img alt="image" border="0" height="294" src="http://lh4.ggpht.com/-AEDoxg844Uk/UJOC--GBQ7I/AAAAAAAADP0/yblZeuEtqCU/image_thumb%25255B10%25255D.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="520" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see the strings used are shown as the test name making it easy to understand what is being tested.&lt;br /&gt;
&lt;h3&gt;
Wait there’s more&lt;/h3&gt;
One problem I always have is tests that test more than one assertion. The problem is that if one assertion fail we cannot know what is the status with the other assertions being tested. &lt;a href="https://bitbucket.org/johannesrudolph/subspec/wiki/Home" target="_blank"&gt;SubSpec&lt;/a&gt; has a cool solution – run each assertion as a different test:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;[Specification]
public void StoreWithOneProduct()
{
    const string productId = "prod-1";

    var store = default(Store);

    "Given a new store".Context(() =&amp;gt; store = new Store());

    "with one product".Do(() =&amp;gt; store.AddProduct(productId));

    // Usually this line would cause the line after it to fail
    "Customer can purchase that product".Assert(
        () =&amp;gt; Assert.True(store.BuyProduct(productId)));

    "The store has that product".Assert(() =&amp;gt; Assert.True(store.HasProduct(productId)));
}&lt;/pre&gt;
&lt;br /&gt;
&lt;a href="http://lh4.ggpht.com/-L1aAE2PVhTw/UJOC_8XlFBI/AAAAAAAADQA/ebmSrh7NNq0/s1600-h/image%25255B22%25255D.png"&gt;&lt;img alt="image" border="0" height="333" src="http://lh6.ggpht.com/-JucdcKcI7wc/UJODAkYkFvI/AAAAAAAADQE/QQAJaz5d3TI/image_thumb%25255B12%25255D.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="521" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
SubSpec has actually run two separate tests one for each assertion. But in case you want a proof – let’s write a test with a failing assertion:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;[Specification]
public void StoreWithOneProduct()
{
    const string productId = "prod-1";

    var store = default(Store);

    "Given a new store".Context(() =&amp;gt; store = new Store());

    "with one product and product is purchased".Do(() =&amp;gt; 
        {
            store.AddProduct(productId);
            store.BuyProduct(productId);
        });

    // this is wrong - stop copy/pasting your tests :)
    "Customer can purchase that product".Assert(
        () =&amp;gt; Assert.True(store.BuyProduct(productId)));

    "The store does not have that product".Assert(() =&amp;gt; Assert.False(store.HasProduct(productId)));
}&lt;/pre&gt;
&lt;br /&gt;
And the result of this test run is:&lt;br /&gt;
&lt;a href="http://lh6.ggpht.com/-1ggtHLkPARY/UJODBr20N9I/AAAAAAAADQQ/z4f4dHBkHj0/s1600-h/image%25255B30%25255D.png"&gt;&lt;img alt="image" border="0" height="452" src="http://lh6.ggpht.com/-uIYac5yqrKw/UJODCp502xI/AAAAAAAADQY/4LGvE6an6Io/image_thumb%25255B16%25255D.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="666" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
The 2nd assertion passed since it was run independently from the 1st failing assertion.&lt;br /&gt;
&lt;h3&gt;
Running all of the assertions in one test&lt;/h3&gt;
What if you do need to run all of the assertions in one test? Simple, use &lt;em&gt;Observe&lt;/em&gt; keyword that cause all of the assertions created within it to run in the same context.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;[Specification]
public void PushSpecification()
{
    var stack = default(Stack&lt;string&gt;);
    "Given a new stack".Context(() =&amp;gt; stack = new Stack&lt;string&gt;());

    string element = "first element";
    "with an element pushed onto it".Do(() =&amp;gt; stack.Push(element));

    "the stack is not empty".Observe(() =&amp;gt; Assert.False(stack.IsEmpty));
    "the stacks Top is the pushed element".Observe(() =&amp;gt; Assert.Equals(element, stack.Top));
}&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="SubSpec Assert" src="http://bitbucket.org/johannesrudolph/subspec/wiki/img/subspec_observe1-e1283077433506.png" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
And yes – you can mix &lt;em&gt;Assert&lt;/em&gt; and &lt;em&gt;Observe&lt;/em&gt; in the same test – and Godspeed on that.&lt;br /&gt;
&lt;h3&gt;
Is that All&lt;/h3&gt;
Yes!&lt;br /&gt;
&lt;br /&gt;
This is all you need to know in order to use SubSpec, there’s nothing more. It’s simple compact and works. &lt;a href="http://jorudolph.wordpress.com/" target="_blank"&gt;Johannes Rudolph&lt;/a&gt; - the creator of SubSpec has taken pains not to add anything else to his frameworks keeping it simple and very small.&lt;br /&gt;
But so far I failed to find any limitation that prevents me from using SubSpec and XUnit to quickly add behaviors to my tests.&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=xgxov8_zqRA:DzIGiajehBc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=xgxov8_zqRA:DzIGiajehBc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=xgxov8_zqRA:DzIGiajehBc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=xgxov8_zqRA:DzIGiajehBc:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=xgxov8_zqRA:DzIGiajehBc:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=xgxov8_zqRA:DzIGiajehBc:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=xgxov8_zqRA:DzIGiajehBc:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=xgxov8_zqRA:DzIGiajehBc:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/xgxov8_zqRA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/xgxov8_zqRA/unit-level-bdd-using-subspec.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-MvPpkHK_gI4/UJOCr_BMRNI/AAAAAAAADPY/XsXdEHq4HEc/s72-c/image_thumb%25255B1%25255D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/11/unit-level-bdd-using-subspec.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-3575800687632059691</guid><pubDate>Thu, 18 Oct 2012 15:29:00 +0000</pubDate><atom:updated>2012-10-20T21:48:00.294+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Agile</category><category domain="http://www.blogger.com/atom/ns#">TDD</category><title>Let’s do TDD–take #2</title><description>I’ve been preaching about the benefits of &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank"&gt;Test Driven Development&lt;/a&gt; (TDD) for a long time and for the last two years I’ve been trying to introduce the idea to my team with limited success – some of my co-workers embraced TDD while other "just didn’t get it”.&lt;br /&gt;
&lt;a href="http://lh4.ggpht.com/-J0ip96QZVOw/UIAgJTxyQ1I/AAAAAAAADMs/O4CwrGW5IKk/s1600-h/image3.png"&gt;&lt;img align="left" alt="image" border="0" height="216" src="http://lh6.ggpht.com/-88RghONl0cY/UIAgKg2Dr6I/AAAAAAAADM0/9i6OEitoNyA/image_thumb1.png?imgmax=800" style="background-image: none; border-bottom-width: 0px; border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; display: inline; float: left; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="240" /&gt;&lt;/a&gt;At first I become frustrated and I’ve tried explaining more and more about it without any real success. When I asked several of my teammates why they fail to even try writing tests before code the answer usually was – “I would but I don’t have enough time for it right now” or “it’s a good idea but it won’t work with this feature”.&lt;br /&gt;
One day it hit me – why not get the team to do TDD in a natural environment where they can try it without fear of missing a deadline or writing bad code. And so I gathered the team together in one room for an hour and a half of “TDD training”.&lt;br /&gt;
For the exercise I’ve chosen a problem that is simple to solve in small iterative steps (TDD) but very hard to design up front.&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;The exercise I've used was a roman numeral to decimal convertor. it is a well known problem that luckily for me none of my team had the pleasure of solving in the past.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New, Courier, monospace;"&gt;The &lt;a href="https://docs.google.com/open?id=0B4IdlbTtcV1ARENna2dYT0Zvbms" target="_blank"&gt;slides from this session are available here&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
After a short introduction of the problem and a few slides on the subject of TDD I got the team to pair up and try and solve that problem.&lt;br /&gt;
&lt;a href="http://lh6.ggpht.com/-Pa9oNZUNG3U/UIAgMkQTAZI/AAAAAAAADM8/WZ72AzPbzlM/s1600-h/image%25255B5%25255D.png"&gt;&lt;img align="right" alt="image" border="0" height="229" src="http://lh3.ggpht.com/-rok61JNTOew/UIAgOaDtqOI/AAAAAAAADNA/ku27d1Ji35w/image_thumb%25255B2%25255D.png?imgmax=800" style="background-image: none; border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: inline; float: right; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="240" /&gt;&lt;/a&gt;&lt;br /&gt;
It was a fun exercise and some of my co-workers got to try test first for the first time.&lt;br /&gt;
I loved the fact The pair that finished first really embraced the methodology. The cool thing is that one member of that pair refused to try TDD for the last year.&lt;br /&gt;
We had so much fun that we’ve decided as a team to have this kind of meeting every three weeks.&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N75wbwoE3lw:YXW3Fw0yFL4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N75wbwoE3lw:YXW3Fw0yFL4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=N75wbwoE3lw:YXW3Fw0yFL4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N75wbwoE3lw:YXW3Fw0yFL4:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N75wbwoE3lw:YXW3Fw0yFL4:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N75wbwoE3lw:YXW3Fw0yFL4:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=N75wbwoE3lw:YXW3Fw0yFL4:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=N75wbwoE3lw:YXW3Fw0yFL4:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/N75wbwoE3lw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/N75wbwoE3lw/lets-do-tddtake-2.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-88RghONl0cY/UIAgKg2Dr6I/AAAAAAAADM0/9i6OEitoNyA/s72-c/image_thumb1.png?imgmax=800" height="72" width="72" /><thr:total>9</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/10/lets-do-tddtake-2.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-19182840678740601</guid><pubDate>Mon, 08 Oct 2012 19:46:00 +0000</pubDate><atom:updated>2012-10-08T21:46:44.216+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Presentation</category><category domain="http://www.blogger.com/atom/ns#">Agile</category><title>DevReach 2012 session</title><description>&lt;p&gt;Last week I had the pleasure being one of the speakers at &lt;a href="http://www.devreach.com" target="_blank"&gt;DevReach&lt;/a&gt;. I like to thank everybody who attended my talk - “Creating change from within”. I had a full room (the smaller theater) and got good feedback (and questions) from the attendees.&lt;/p&gt; &lt;iframe height="400" marginheight="0" src="http://www.slideshare.net/slideshow/embed_code/14640583?hostedIn=slideshare&amp;amp;page=upload" frameborder="0" width="476" marginwidth="0" scrolling="no"&gt;&lt;/iframe&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;It was a fun, well organized convention and I met some very interesting people – I hope I’ll get a chance to return next year.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=sRmtNcUGsXc:APv13ER4KMM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=sRmtNcUGsXc:APv13ER4KMM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=sRmtNcUGsXc:APv13ER4KMM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=sRmtNcUGsXc:APv13ER4KMM:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=sRmtNcUGsXc:APv13ER4KMM:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=sRmtNcUGsXc:APv13ER4KMM:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=sRmtNcUGsXc:APv13ER4KMM:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=sRmtNcUGsXc:APv13ER4KMM:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/sRmtNcUGsXc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/sRmtNcUGsXc/devreach-2012-session.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/10/devreach-2012-session.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-8898218130699087263</guid><pubDate>Wed, 19 Sep 2012 17:24:00 +0000</pubDate><atom:updated>2012-09-19T20:24:34.139+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Presentation</category><category domain="http://www.blogger.com/atom/ns#">Agile</category><title>I’m going to DevReach</title><description>&lt;p&gt;I’m so excited - I’ll be going to DevReach 2012 – as a speaker!&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.devreach.com/speakers/dror-helper" target="_blank"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="DevReach 2012 - Speaker Badge" border="0" alt="DevReach 2012 - Speaker Badge" align="left" src="http://lh4.ggpht.com/-dNOckFB1VfY/UFn_zm-84mI/AAAAAAAADLw/xItWih7NwLc/DevReach%2525202012%252520-%252520Speaker%252520Badge%25255B9%25255D.jpg?imgmax=800" width="158" height="109" /&gt;&lt;/a&gt;One of my sessions have been selected for &lt;a href="http://www.devreach.com" target="_blank"&gt;DevReach&lt;/a&gt;. On the 4th of October at exactly 13:45 I’ll be speaking about how to facilitate change in your dev team.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So if you’re coming to DevReach don’t forget to come by and say hello.&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=mITuAJyQYP4:3gYONM6H2Sg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=mITuAJyQYP4:3gYONM6H2Sg:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=mITuAJyQYP4:3gYONM6H2Sg:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=mITuAJyQYP4:3gYONM6H2Sg:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=mITuAJyQYP4:3gYONM6H2Sg:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=mITuAJyQYP4:3gYONM6H2Sg:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=mITuAJyQYP4:3gYONM6H2Sg:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=mITuAJyQYP4:3gYONM6H2Sg:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/mITuAJyQYP4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/mITuAJyQYP4/im-going-to-devreach.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-dNOckFB1VfY/UFn_zm-84mI/AAAAAAAADLw/xItWih7NwLc/s72-c/DevReach%2525202012%252520-%252520Speaker%252520Badge%25255B9%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/09/im-going-to-devreach.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-1808970379724365326</guid><pubDate>Mon, 17 Sep 2012 19:44:00 +0000</pubDate><atom:updated>2012-09-17T22:47:38.003+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">DSL</category><category domain="http://www.blogger.com/atom/ns#">Tips and Tricks</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Fluent interfaces in C# – method chaining</title><description>For those of you who just tuned in, this is a third post on the subject of fluent interfaces using C#. In case you haven’t read them before – here are my previous posts on the subject:&lt;br /&gt;
&lt;blockquote&gt;
&lt;a href="http://blog.drorhelper.com/2012/07/fluent-interfaces-in-cintroduction.html"&gt;Introduction&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://blog.drorhelper.com/2012/07/fluent-interfaces-in-c-extension.html"&gt;Extension Methods&lt;/a&gt;&lt;/blockquote&gt;
Right, now that we’re familiar with fluent interfaces it’s time to move to “Method Chaining”.&lt;br /&gt;
Method chaining is the most recognized “fluent interface technique”. In fact it’s so popular that some believe it to be the only way to write a fluent API.&lt;br /&gt;
The idea behind method chaining is both simple and elegant – each method call returns an object that can be used to call another method that returns an object an so on.&lt;br /&gt;
&lt;h3&gt;
Returning “this”&lt;/h3&gt;
I guess that an example is in order – unless you’ve started writing .NET code yesterday you should be familiar with the &lt;em&gt;StringBuilder&lt;/em&gt; class:&lt;br /&gt;
&lt;blockquote&gt;
In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new &lt;a href="http://msdn.microsoft.com/en-us/library/system.string(v=vs.100).aspx"&gt;String&lt;/a&gt; object can be costly. The &lt;a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.100).aspx"&gt;System.Text.StringBuilder&lt;/a&gt; class can be used when you want to modify a string without creating a new object. For example, using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder(v=vs.100).aspx"&gt;StringBuilder&lt;/a&gt; class can boost performance when concatenating many strings together in a loop. &lt;br /&gt;
[From &lt;a href="http://msdn.microsoft.com/en-us/library/2839d5h5(v=vs.100).aspx"&gt;MSDN&lt;/a&gt;]&lt;/blockquote&gt;
Using the &lt;em&gt;StringBuilder&lt;/em&gt; class is simple:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;var sb = new StringBuilder();
sb.Append("This is a simple list");
sb.AppendLine();
sb.Append("1. First item: ");
sb.Append(items[0]);
sb.AppendLine();
sb.Append("2. Second item: ");
sb.Append(items[1]);&lt;/pre&gt;
The code above can also be written like this:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true;"&gt;var sb = new StringBuilder()
    .Append("This is a simple list")
    .AppendLine()
    .Append("1. First item: ").Append(items[0])
    .AppendLine()
    .Append("2. Second item: ").Append(items[1]);&lt;/pre&gt;
It’s up to you to decide which is more readable (#2) the fact is that writing the 2nd example is takes less time (try it), just keep using “.” after each method call.&lt;br /&gt;
The method chaining “trick” is in the return value of the method. In this case each method call above returns &lt;em&gt;this&lt;/em&gt; at the end:&lt;br /&gt;
&lt;pre class="brush: csharp; ruler: true; highlight: [9];"&gt;[SecuritySafeCritical]
public unsafe StringBuilder Append(string value)
{
    if (value != null)
    {
        // Here be code!
    }

    return this;
}&lt;/pre&gt;
This means that we can invoke more methods on the same class one after the other.&lt;br /&gt;
Returning the same &lt;em&gt;type&lt;/em&gt; on each method call means that the API is “stateless” – meaning that I can always choose the same methods regardless of the last method invoked.&lt;br /&gt;
&lt;h3&gt;
Conclusion&lt;/h3&gt;
In this post simple method chaining was introduced using return value of &lt;em&gt;this&lt;/em&gt; in order to enable the user of the API to call the same class methods in one chain.&lt;br /&gt;
Keep in mind that this is only one technique - there are other ways to create fluent API that uses method chaining and I promise to write more about it in another post.&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=vdfP-Bnm3qg:mH6UiM84HNY:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=vdfP-Bnm3qg:mH6UiM84HNY:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=vdfP-Bnm3qg:mH6UiM84HNY:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=vdfP-Bnm3qg:mH6UiM84HNY:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=vdfP-Bnm3qg:mH6UiM84HNY:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=vdfP-Bnm3qg:mH6UiM84HNY:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=vdfP-Bnm3qg:mH6UiM84HNY:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=vdfP-Bnm3qg:mH6UiM84HNY:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/vdfP-Bnm3qg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/vdfP-Bnm3qg/fluent-interfaces-in-c-method-chaining.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/09/fluent-interfaces-in-c-method-chaining.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-2719970431145398727</guid><pubDate>Mon, 03 Sep 2012 20:04:00 +0000</pubDate><atom:updated>2012-09-03T23:04:52.204+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Better Place</category><title>Driving in my (electric) car</title><description>&lt;p&gt;If you do not know already (why should you?) I ‘m working at &lt;a href="http://www.betterplace.com/" target="_blank"&gt;Better Place&lt;/a&gt;. &lt;img style="display: inline; float: right" align="right" src="http://www.zivaveng.com/files/images/282.jpg" width="123" height="125" /&gt;&lt;/p&gt;  &lt;p&gt;My company’s mission is to replace old fashioned, gas fueled cars with electric cars. And we do that by providing the infrastructure that (will) enable drivers to drive their (electric) car wherever they want to go to.&lt;/p&gt;  &lt;p&gt;A few months ago I’ve decided to make the move and replace my leased car with an electric one – and last Thursday I got my very own electric car.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-b0qhBqt99uQ/UEUNPsDXmmI/AAAAAAAADK4/wvRFSwqx2LQ/s1600-h/-10.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="דרור הלפר" border="0" alt="דרור הלפר" src="http://lh5.ggpht.com/-23HWnKiBDC0/UEUNQ5uolCI/AAAAAAAADLA/0yxCr5LvIeI/-_thumb6.jpg?imgmax=800" width="471" height="291" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;SO what do I do at Better Place you ask? in a nutshell my team is responsible for the software in the &lt;a href="http://www.betterplace.com/the-solution-switch-stations" target="_blank"&gt;battery switch stations&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-ym6i47rNEsg/UEUNUL1S59I/AAAAAAAADLI/s_vWqfmdbDI/s1600-h/IMG_081011.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="IMG_0810" border="0" alt="IMG_0810" align="left" src="http://lh6.ggpht.com/--p048nYIaFk/UEUNVY1HRqI/AAAAAAAADLQ/BUI3pDxv1qU/IMG_0810_thumb8.jpg?imgmax=800" width="260" height="200" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Which I had first hand experience with in the last weekend.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-IfQwmjg3M58/UEUNXbeZx5I/AAAAAAAADLY/gNgQEeWCcxY/s1600-h/IMG_081117.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="IMG_0811" border="0" alt="IMG_0811" src="http://lh4.ggpht.com/-8oB17uGPcvQ/UEUNYvucG4I/AAAAAAAADLg/AspccSJm5gc/IMG_0811_thumb5.jpg?imgmax=800" width="260" height="200" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;So far it has been a fun ride – and I hope it would continue for a long time.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;In the meantime – Happy coding…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=SWAK8XYxMJ4:XdeaF0tY4yU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=SWAK8XYxMJ4:XdeaF0tY4yU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=SWAK8XYxMJ4:XdeaF0tY4yU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=SWAK8XYxMJ4:XdeaF0tY4yU:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=SWAK8XYxMJ4:XdeaF0tY4yU:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=SWAK8XYxMJ4:XdeaF0tY4yU:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=SWAK8XYxMJ4:XdeaF0tY4yU:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=SWAK8XYxMJ4:XdeaF0tY4yU:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/SWAK8XYxMJ4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/SWAK8XYxMJ4/driving-in-my-electric-car.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-23HWnKiBDC0/UEUNQ5uolCI/AAAAAAAADLA/0yxCr5LvIeI/s72-c/-_thumb6.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/09/driving-in-my-electric-car.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-8483804285891114670</guid><pubDate>Mon, 16 Jul 2012 15:25:00 +0000</pubDate><atom:updated>2012-07-16T18:25:08.564+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">DSL</category><category domain="http://www.blogger.com/atom/ns#">Tips and Tricks</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Fluent Interfaces in C# – Extension Methods</title><description>&lt;p&gt;FOr those of you who haven't read the previous post. This post is the 2nd of many where I explain how to use C# and a bag of tricks to create fluent interfaces – easily.&lt;/p&gt;  &lt;p&gt;In the &lt;a href="http://blog.drorhelper.com/2012/07/fluent-interfaces-in-cintroduction.html" target="_blank"&gt;previous post&lt;/a&gt; I’ve talked about what fluent interfaces is all about and gave a brief introduction to the subject – in this post we’ll actually get to see some code.&lt;/p&gt;  &lt;h3&gt;&lt;/h3&gt;  &lt;h3 align="left"&gt;The introduction you might not need&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p align="left"&gt;Extension methods enable you to &amp;quot;add&amp;quot; methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.&lt;/p&gt;    &lt;p align="left"&gt;from &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" target="_blank"&gt;C# Programming Guide&lt;/a&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p align="left"&gt;First introduced in .NET 3 – extension methods have been widely used to “add” methods to existing class without changing the actual class implementation. Using extension methods is as simple as creating a static method – in fact that’s all it takes.&lt;/p&gt;  &lt;h3 align="left"&gt;Using extension methods to create an API&lt;/h3&gt;  &lt;p align="left"&gt;The quick and simple way to create “poor man’s” fluent API is to use Extension methods. &lt;/p&gt;  &lt;p align="left"&gt;All we need is to create a few static methods and we can transform the following code:&lt;/p&gt;  &lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt;   &lt;div style="padding-bottom: 2px; background-color: #ffffff; padding-left: 5px; padding-right: 5px; white-space: nowrap; max-height: 300px; overflow: auto; padding-top: 2px"&gt;&lt;span style="color: #008000"&gt;// Instead of using this:&lt;/span&gt;       &lt;br /&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.AddDays(14);       &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: #008000"&gt;// Using extension method we can write:&lt;/span&gt;       &lt;br /&gt;&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.AddWeeks(2);       &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: #008000"&gt;// Or even this:&lt;/span&gt;       &lt;br /&gt;14.DaysFromNow();&lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;The actual implementation is quite simple – but here it is just in case:&lt;/p&gt;  &lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt;   &lt;div style="padding-bottom: 2px; background-color: #ffffff; padding-left: 5px; padding-right: 5px; white-space: nowrap; overflow: auto; padding-top: 2px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; AddWeeks(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; baseTime, &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; weeks)       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; baseTime.AddDays(7 * weeks);       &lt;br /&gt;}       &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; DaysFromNow(&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; days)       &lt;br /&gt;{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now.AddDays(days);       &lt;br /&gt;}&lt;/div&gt; &lt;/div&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Thus by using simple method call we managed to transform “14” to “two weeks” – not much but it’s a good place to start.&lt;/p&gt;  &lt;h3&gt;When to use&lt;/h3&gt;  &lt;p&gt;Extension methods are best used when the API we’re trying to add is on top some 3rd party component or internal class we do not wish to use.&lt;/p&gt;  &lt;p&gt;This method is very effective when using along with “method chaining” where using extension method is a good starting point for the whole API – but more on that in posts to come.&lt;/p&gt;  &lt;p&gt;In the meantime keep in mind that one of the best fluent API out there (in my humble opinion) uses extension method extensively:&lt;/p&gt;  &lt;div style="border-bottom: #000080 1px solid; border-left: #000080 1px solid; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; color: #000; font-size: 10pt; border-top: #000080 1px solid; border-right: #000080 1px solid"&gt;   &lt;div style="padding-bottom: 2px; background-color: #ffffff; padding-left: 5px; padding-right: 5px; white-space: nowrap; max-height: 500px; overflow: auto; padding-top: 2px"&gt;     &lt;p&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; Where&amp;lt;TSource&amp;gt;         &lt;br /&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; (&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source,         &lt;br /&gt;&lt;span style="color: #2b91af"&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; Func&lt;/span&gt;&amp;lt;TSource, &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt;&amp;gt; predicate)&lt;/p&gt;      &lt;p&gt;       &lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TResult&amp;gt; Select&amp;lt;TSource, TResult&amp;gt;         &lt;br /&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; (&lt;span style="color: #0000ff"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSource&amp;gt; source,         &lt;br /&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; &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TSource, TResult&amp;gt; selector)&lt;/p&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;h3&gt;Cons and pitfalls&lt;/h3&gt;  &lt;p&gt;There are some problems in “adding methods” to existing class – especially if this class is part of the CLR. In the example above I’ve used extension method on &lt;em&gt;integer&lt;/em&gt; to create a new &lt;em&gt;DateTime&lt;/em&gt; which has nothing to do with the previous type. I’ve added as method to every single &lt;em&gt;integer &lt;/em&gt;in my system. Although I could filter the the use of the new method using &lt;em&gt;namespace&lt;/em&gt; it’s still a lot of noise.&lt;/p&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;This is it – the first tool in your fluent interface belt. Experiment with it and use it wisely. And as always:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Happy Coding…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=MfMoWDyR0PY:9CaVpiqj1UE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=MfMoWDyR0PY:9CaVpiqj1UE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=MfMoWDyR0PY:9CaVpiqj1UE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=MfMoWDyR0PY:9CaVpiqj1UE:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=MfMoWDyR0PY:9CaVpiqj1UE:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=MfMoWDyR0PY:9CaVpiqj1UE:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=MfMoWDyR0PY:9CaVpiqj1UE:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=MfMoWDyR0PY:9CaVpiqj1UE:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/MfMoWDyR0PY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/MfMoWDyR0PY/fluent-interfaces-in-c-extension.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>1</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/07/fluent-interfaces-in-c-extension.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-7749136202623344697</guid><pubDate>Mon, 02 Jul 2012 18:57:00 +0000</pubDate><atom:updated>2012-07-02T21:57:48.382+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DSL</category><category domain="http://www.blogger.com/atom/ns#">C#</category><title>Fluent Interfaces in C#–Introduction</title><description>&lt;p&gt;Not many developers are familiar with the concept of DSLs (Domain Specific Languages) even fewer use them in their day job. It seems that creating a new language is a hard job that should only be left for professors in the academia or certain employees that work in Microsoft, Sun or some other compiler vendor.&lt;/p&gt;  &lt;p&gt;But not all languages are created equal in the world of DSLs, there is a branch of languages called “Internal DSLs” – that any developer worth his salt can leverage to create better, more readable code.&lt;/p&gt;  &lt;h3&gt;Internal DSLs and fluent interfaces&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;The basic idea of a domain specific language (DSL) is a computer language that's targeted to a particular kind of problem, rather than a general purpose language that's aimed at any kind of software problem. Domain specific languages have been talked about, and used for almost as long as computing has been done.&lt;/p&gt;    &lt;p&gt;&lt;em&gt;Martin Fowler – &lt;a href="http://martinfowler.com/bliki/DomainSpecificLanguage.html" target="_blank"&gt;DomainSpecificLanguage&lt;/a&gt;&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And so internal DSL (a.k.a “Embedded DSL) is a domain specific language created using an existing (host) language.&lt;/p&gt;  &lt;p&gt;Within the group of internal DSLs there is sub group known as “fluent interfaces” – basically using the host language to create code that can be easily read – and look (after a fashion) just like plain English.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-Eny8feF2tKk/T_HvKIZqAWI/AAAAAAAADKY/psHE1cLz5jo/s1600-h/image1.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh3.ggpht.com/-jnKVlPp1UfM/T_HvK7N2WDI/AAAAAAAADKg/lTca4jN5YZs/image_thumb.png?imgmax=800" width="517" height="347" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The term “Fluent Interfaces” was coined by &lt;a href="http://martinfowler.com/bliki/FluentInterface.html" target="_blank"&gt;Eric Evens and Martin Fowler&lt;/a&gt; and it refers to an implementation of object oriented API on top of existing functionality in order to provider more readable code.&lt;/p&gt;  &lt;h3&gt;Why should you care?&lt;/h3&gt;  &lt;p&gt;There are many benefits of using fluent interfaces – you can create a easy to use and understand API that does not require any additional libraries to implement.&lt;/p&gt;  &lt;p&gt;That API provides directions on how the code should be used and can help your users use your software or you can use it internally in order to make a complicated process easy to code by your teammates.&lt;/p&gt;  &lt;p&gt;There is one catch – creating a good API is always difficult and creating fluent interface requires understanding of certain tricks in the language that can be leveraged to create the desired code.&lt;/p&gt;  &lt;p&gt;C# just like any other object oriented language has the capability to create fluent API in fact certain feature of C# makes it extremely easy to create such API.&lt;/p&gt;  &lt;h3&gt;What's next&lt;/h3&gt;  &lt;p&gt;In the upcoming posts I’m going to show a few tricks that I’ve used in the past to create fluent interfaces all of which you probably already know.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;So until next time – happy coding…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=nKDir3yOfn8:BEUtxluQZJ4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=nKDir3yOfn8:BEUtxluQZJ4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=nKDir3yOfn8:BEUtxluQZJ4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=nKDir3yOfn8:BEUtxluQZJ4:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=nKDir3yOfn8:BEUtxluQZJ4:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=nKDir3yOfn8:BEUtxluQZJ4:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=nKDir3yOfn8:BEUtxluQZJ4:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=nKDir3yOfn8:BEUtxluQZJ4:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/nKDir3yOfn8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/nKDir3yOfn8/fluent-interfaces-in-cintroduction.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-jnKVlPp1UfM/T_HvK7N2WDI/AAAAAAAADKg/lTca4jN5YZs/s72-c/image_thumb.png?imgmax=800" height="72" width="72" /><thr:total>3</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/07/fluent-interfaces-in-cintroduction.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-7989709787521804920</guid><pubDate>Wed, 30 May 2012 19:17:00 +0000</pubDate><atom:updated>2012-05-30T22:17:27.337+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Git</category><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">nDepend</category><title>Software news – May 2012</title><description>&lt;p&gt;Again it’s that time! I have some cool new/updates software I want to tell you about. This time I got three products for the .NET developer that were just released.&lt;/p&gt;  &lt;h3&gt;nDepend V4&lt;/h3&gt;  &lt;p&gt;A new &lt;a href="http://www.ndepend.com" target="_blank"&gt;nDepend&lt;/a&gt; version was just released with a few features but I just got to play around with two of them:&lt;/p&gt;  &lt;p&gt;The first new feature is &lt;a href="http://www.ndepend.com/Features.aspx#CQL" target="_blank"&gt;CQLinq&lt;/a&gt; is! In the past I’ve used CQL (Code Query language) to create &lt;a href="http://www.ndepend.com" target="_blank"&gt;nDepend&lt;/a&gt; rules. from now on I’m going to use the new Linqish derivative that enable me to do much much more.&lt;/p&gt;  &lt;p&gt;&lt;img src="http://www.ndepend.com/Res/CQLEdition2.PNG" /&gt;&lt;/p&gt;  &lt;p&gt;Other new big feature is the &lt;a href="http://www.ndepend.com/Features.aspx#API" target="_blank"&gt;nDepend API&lt;/a&gt; along with power tools – with full &lt;a href="http://www.ndepend.com/API/webframe.html?NDepend.API_introduction.html" target="_blank"&gt;documentation&lt;/a&gt; and code examples on how you can use the new API to make your code better.&lt;/p&gt;  &lt;h3&gt;GitHub for windows&lt;/h3&gt;  &lt;p&gt;I’ve been planning to host my next pet project on &lt;a href="http://www.ndepend.com/Features.aspx#API" target="_blank"&gt;GitHub&lt;/a&gt; for some time. I’ve found tutorials and screencasts and there goes these guys at GitHub and create a working, easy-to-use windows client – &lt;a href="http://windows.github.com/" target="_blank"&gt;github:windows&lt;/a&gt;.&lt;a href="http://lh6.ggpht.com/-u811WmUXCxk/T8ZyF7X8bJI/AAAAAAAADKA/U4GYs5nv5O0/s1600-h/image%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" align="right" src="http://lh3.ggpht.com/-itOz80CRMDU/T8ZyP495mmI/AAAAAAAADKI/_pPrDsw_lu8/image_thumb%25255B1%25255D.png?imgmax=800" width="240" height="137" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;This new tool bring one install to rule the all, that puts shell as well a well crafted, easy to use and visually compelling windows (WPF) client.&lt;/p&gt;  &lt;p&gt;You still to know what you’re doing but using Git has just got a lot easier for windows users.&lt;/p&gt;  &lt;h3&gt;SharpDevelop 4.2&lt;/h3&gt;  &lt;p&gt;The lightweight, free (open sourced) .NET IDE has just celebrated another release. In this release adds .NET 4.5 targeting support, ASP.MVC 3 support, &lt;a href="http://community.sharpdevelop.net/blogs/mattward/archive/2011/11/09/MachineSpecifications.aspx" target="_blank"&gt;MSpec integration&lt;/a&gt;, and more &lt;a href="http://community.sharpdevelop.net/blogs/mattward/archive/2011/10/05/SubsonicT4Templates.aspx" target="_blank"&gt;T4 template support&lt;/a&gt; (which I wish for in Visual Studio). There's more to this release just head for the official &lt;a href="http://community.sharpdevelop.net/forums/t/15772.aspx" target="_blank"&gt;release notes page&lt;/a&gt; for more details.&lt;/p&gt;  &lt;p&gt;If you’re looking for a good Visual Studio alternative – for development at home (or maybe small business) have a look – you might like it.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Happy coding…&lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=zMkYXjof-P0:XjfsSfINskM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=zMkYXjof-P0:XjfsSfINskM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=zMkYXjof-P0:XjfsSfINskM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=zMkYXjof-P0:XjfsSfINskM:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=zMkYXjof-P0:XjfsSfINskM:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=zMkYXjof-P0:XjfsSfINskM:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=zMkYXjof-P0:XjfsSfINskM:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=zMkYXjof-P0:XjfsSfINskM:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/zMkYXjof-P0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/zMkYXjof-P0/software-news-may-2012.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-itOz80CRMDU/T8ZyP495mmI/AAAAAAAADKI/_pPrDsw_lu8/s72-c/image_thumb%25255B1%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/05/software-news-may-2012.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-3738194698281840004</guid><pubDate>Tue, 01 May 2012 06:25:00 +0000</pubDate><atom:updated>2012-05-01T10:12:47.354+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Thoughts</category><title>Are you better at math or code?</title><description>&lt;blockquote&gt;
“Choose a number from 1 to 10” – they usually choose 5, I’m not sure why.&lt;/blockquote&gt;
This how an interview question I’ve used to ask begins…&lt;br /&gt;
&lt;h3&gt;
The question&lt;/h3&gt;
Suppose you’re writing a client that receive a message with a number N (usually 5). Next you’ll receive N messages each contains a different number from 1 to N &lt;br /&gt;
&lt;blockquote&gt;
&lt;strong&gt;1 2 3 4 5 &lt;/strong&gt;&lt;/blockquote&gt;
Simple. At this point the interviewee start looking for a catch, but being a kind soul that I am I reassure him that it’s yet to come…&lt;br /&gt;
Now to make things interesting the ordering of the messages (i.e. numbers) is not defined – in face you can receive the numbers in any order.&lt;br /&gt;
&lt;blockquote&gt;
&lt;strong&gt;For example I could receive 2 3 4 1 5 or 3 5 1 3 4 &lt;/strong&gt;&lt;/blockquote&gt;
Still here? &lt;br /&gt;
And finally we know for certain that one of the numbers will &lt;u&gt;not&lt;/u&gt; be sent while another will be sent twice&lt;br /&gt;
&lt;blockquote&gt;
&lt;strong&gt;For example: 2 3 &lt;em&gt;1&lt;/em&gt; 5 &lt;em&gt;1&lt;/em&gt;&lt;/strong&gt;&lt;/blockquote&gt;
That’s it. So now design an algorithm that will return two pieces of data – which number was not sent and which was sent twice. in the last example the output should be &lt;em&gt;4 &lt;/em&gt;and &lt;em&gt;1.&lt;/em&gt;&lt;br /&gt;
That a few minutes to think on how to solve this problem.&lt;br /&gt;
&lt;br /&gt;
The reason I used to ask this question is that you can learn quite a lot by the way that this question is answered. The trick is that it’s actually two separate questions:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Which number appeared twice &lt;/li&gt;
&lt;li&gt;Which number was not sent &lt;/li&gt;
&lt;/ol&gt;
As software developers we’re expected to have some math skills and (hopefully) can code.&lt;br /&gt;
I’ve noticed that developers with strong math skill usually answer one first while developers that feel more comfortable with code choose to answer the other. Most answered both but it’s the way that counts.&lt;br /&gt;
Note: Just to make things clear – I believe that good developers are good in both fields but usually are better at one than the other.&lt;br /&gt;
Are you ready for the answer(s)? here it is:&lt;br /&gt;
&lt;h3&gt;
Which number appeared twice&lt;/h3&gt;
To find out which number appears twice you’ll need an array of size N (which we know) of Boolean or integers and each time a number arrive just go to that number’s index (or index – 1 because the array starts with ‘0’) if it’s marked &lt;em&gt;false&lt;/em&gt; (or &lt;em&gt;0&lt;/em&gt;) just change that cell value to &lt;em&gt;true&lt;/em&gt; (or &lt;em&gt;1&lt;/em&gt;) but if it was already previously changed you know that this is the number that appears twice – simple.&lt;br /&gt;
I found the developers that are stronger in code find this solution faster then the other.&lt;br /&gt;
&lt;h3&gt;
Which number was not sent&lt;/h3&gt;
Most math centric developers quickly look at the input and see that it’s an &lt;a href="http://en.wikipedia.org/wiki/Arithmetic_progression" target="_blank"&gt;arithmetic progression&lt;/a&gt; – a sequence of consecutive numbers and knowing that they also know that the sum of these numbers can easily be calculated:&lt;br /&gt;
&lt;img alt=" S_n=\frac{n}{2}( a_1 + a_n)." border="0" src="http://upload.wikimedia.org/wikipedia/en/math/a/f/e/afe20f89d7bfdbd0a191168d80eb8077.png" style="background-image: none; border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br /&gt;
The sum on our example (again 1 to 5) is (1 + 5) X 5 / 2 = 15.&lt;br /&gt;
Now we have a sum without the number that appeared twice – no problem. In math there is a trick of assuming that we’ve solved the first question (which number appeared twice) all we need to do is add it to the sum so in our example 1 is added to result in 16.&lt;br /&gt;
After that all you need to do is sum the numbers as they arrive and in the end subtract the new sum from the expected and get the number that was not sent:&lt;br /&gt;
16 – (2 + 3 + 1 + 5 + 1) = 16 – 12 = &lt;strong&gt;4&lt;/strong&gt;.&lt;br /&gt;
&lt;h3&gt;
Conclusion&lt;/h3&gt;
I don’t think that being better in one over the other makes a better programmer the only reason I’ve asked this question is to find if the candidate had basic skills at both and which of these he’s better – a way to understand who am I talking to.&lt;br /&gt;
I hope that you took the time to solve the question before reading the solution so now you know which field you feel more comfortable in.&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=XEY4XW3QYlM:dF_uyXULM8E:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=XEY4XW3QYlM:dF_uyXULM8E:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=XEY4XW3QYlM:dF_uyXULM8E:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=XEY4XW3QYlM:dF_uyXULM8E:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=XEY4XW3QYlM:dF_uyXULM8E:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=XEY4XW3QYlM:dF_uyXULM8E:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=XEY4XW3QYlM:dF_uyXULM8E:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=XEY4XW3QYlM:dF_uyXULM8E:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/XEY4XW3QYlM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/XEY4XW3QYlM/are-you-better-at-math-or-code.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>3</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/05/are-you-better-at-math-or-code.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-5638470768628081856</guid><pubDate>Tue, 10 Apr 2012 19:18:00 +0000</pubDate><atom:updated>2012-04-10T22:23:32.281+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Typemock</category><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><title>Creating robust tests with Isolator V7</title><description>&lt;blockquote&gt;
The problem with unit tests is that they keep on breaking…&lt;/blockquote&gt;
Obviously that’s not entirely correct, nevertheless I had the pleasure of hearing the sentence above numerous times. It’s true – unit tests do tend to fail and we prefer that they fail only when a regression occurs – when something that used to work stopped working, because that’s the reason they’re there in the first place.&lt;br /&gt;
The problem is that there are several instances when a test stops compiling due to requirement change or refactoring. consider the following test code:&lt;br /&gt;
&lt;pre class="brush: csharp;"&gt;[Test]
public void Add_AddTowNumbersInCalculator_ReturnResult()
{
    var fakeDataAccess = Isolate.Fake.Instance&amp;lt;dataAccess&amp;gt;();
    Isolate.WhenCalled(() =&amp;gt; fakeDataAccess.Save(null)).WillReturn(true);

    var customer = new Customer(fakeDataAccess);

    //...

    var result = customer.Save();

    Assert.That(result, Is.True);
}&lt;/pre&gt;
&lt;br /&gt;
Simple. Now what happens when we need to change the &lt;em&gt;Customer&lt;/em&gt; c’tor? – let’s say that it was decided that every customer should have a valid address the developer implement that requirement – like this:&lt;br /&gt;
&lt;pre class="brush: csharp; highlight: 8"&gt;[Test]
public void Add_AddTowNumbersInCalculator_ReturnResult()
{
    var fakeDataAccess = Isolate.Fake.Instance&amp;lt;dataAccess&amp;gt;();
    var fakeAddress = Isolate.Fake.Instance&amp;lt;Address&amp;gt;();
    Isolate.WhenCalled(() =&amp;gt; fakeDataAccess .Save(null)).WillReturn(true);

    var customer = new Customer(fakeAddress, dataAccess);

    //...

    var result = customer.Save();

    Assert.That(result, Is.True);
}&lt;/pre&gt;
&lt;br /&gt;
What if I already wrote a few dozen tests – what can I do?&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Create overloaded c’tor one with parameter and one parameterless&lt;/li&gt;
&lt;li&gt;Add a default value to the c’tor (.NET 4.0)&lt;/li&gt;
&lt;li&gt;Write all of the tests again – from scratch&lt;/li&gt;
&lt;li&gt;Delete your tests and never attempt unit testing your code ever again&lt;/li&gt;
&lt;li&gt;Create a factory method to instantiate &lt;em&gt;Customer&lt;/em&gt; at your tests&lt;/li&gt;
&lt;li&gt;Quit your job and move to another country&lt;/li&gt;
&lt;/ul&gt;
All of the above are valid options (ok, some of these are valid options) they would work great for some of the scenarios. unfortunately most of the time usually you end up re-writing a lot of code each time such a change happen. Adding parameters that you usually do not care about and so you fake them or pass some default value over and over again.&lt;br /&gt;
In fact on my team this problem (needing to fix a lot of tests each time the c’tor signature change) was so severe that one of the developers took&amp;nbsp; it upon himself to create a class that creates object just for the test with all of the parameters faked.&lt;br /&gt;
Luckily there is another way: the new &lt;a href="http://www.typemock.com/isolator-product-page" target="_blank"&gt;Isolator&lt;/a&gt; V7 that was released a few weeks ago and one new feature got me exited (no not the auto-test-runner) – it’s The new API calls: &lt;em&gt;Isolate.Fake.Dependencies &lt;/em&gt;and&lt;em&gt; Isolate.GetFake.&lt;/em&gt;&lt;br /&gt;
Using the new API I’ve fixed the test to work like this:&lt;br /&gt;
&lt;pre class="brush: csharp; "&gt;[Test]
public void Add_AddTowNumbersInCalculator_ReturnResult()
{
    var customer = Isolate.Fake.Dependencies&amp;lt;Customer&amp;gt;();

    var fakeDataAccess = Isolate.GetFake&amp;lt;DataAccess&amp;gt;(customer);
    Isolate.WhenCalled(() =&amp;gt; fakeDataAccess.Save(null)).WillReturn(true);
    //...

    var result = customer.Save();

    Assert.That(result, Is.True);
}&lt;/pre&gt;

I just tell Isolator to fake all dependencies and “grab” the only one that interest me at this test and change it’s behavior – simple.&lt;br /&gt;
And now my test won’t break due to c’tor changes.&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_a19Frp2zdk:CKXR7LUgplA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_a19Frp2zdk:CKXR7LUgplA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=_a19Frp2zdk:CKXR7LUgplA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_a19Frp2zdk:CKXR7LUgplA:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_a19Frp2zdk:CKXR7LUgplA:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_a19Frp2zdk:CKXR7LUgplA:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=_a19Frp2zdk:CKXR7LUgplA:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=_a19Frp2zdk:CKXR7LUgplA:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/_a19Frp2zdk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/_a19Frp2zdk/creating-robust-tests-with-isolator-v7.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/04/creating-robust-tests-with-isolator-v7.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-3966899810108690160</guid><pubDate>Wed, 21 Mar 2012 20:20:00 +0000</pubDate><atom:updated>2012-04-10T22:20:17.159+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">PostSharp</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Presentation</category><category domain="http://www.blogger.com/atom/ns#">AOP</category><title>AOP sessions on IDNDUG</title><description>I’ve just come back from my dual session on AOP on the local user group. The first part was introduction to AOP – what is it and why should you care.&lt;br /&gt;
I’ve also reviewed several ways to implement AOP in your code from functional programming to full AOP frameworks.&lt;br /&gt;
&lt;div id="__ss_12105228" style="width: 425px;"&gt;
&lt;strong style="display: block; margin: 12px 0px 4px;"&gt;&lt;a href="http://www.slideshare.net/dhelper/introduction-to-aop" title="Introduction to aop"&gt;Introduction to aop&lt;/a&gt;&lt;/strong&gt;&lt;object height="355" id="__sse12105228" width="425"&gt;&lt;param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introductiontoaop-120321143232-phpapp02&amp;amp;stripped_title=introduction-to-aop&amp;amp;userName=dhelper" /&gt;
&lt;param name="allowFullScreen" value="true" /&gt;
&lt;param name="allowScriptAccess" value="always" /&gt;
&lt;param name="wmode" value="transparent" /&gt;
&lt;embed name="__sse12105228" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=introductiontoaop-120321143232-phpapp02&amp;amp;stripped_title=introduction-to-aop&amp;amp;userName=dhelper" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="425" height="355"&gt;&lt;/embed&gt;&lt;/object&gt;    &lt;br /&gt;
&lt;div style="padding-bottom: 12px; padding-left: 0px; padding-right: 0px; padding-top: 5px;"&gt;
View more &lt;a href="http://www.slideshare.net/"&gt;presentations&lt;/a&gt; from &lt;a href="http://www.slideshare.net/dhelper"&gt;Dror Helper&lt;/a&gt;.&lt;/div&gt;
&lt;/div&gt;
The source code for this presentation can be &lt;a href="https://skydrive.live.com/redir.aspx?cid=64d349aba764a780&amp;amp;resid=64D349ABA764A780!286&amp;amp;parid=64D349ABA764A780!284&amp;amp;authkey=!APqj08TtlzcQxis" target="_blank"&gt;downloaded from here&lt;/a&gt;.&lt;br /&gt;
The 2nd session was all about &lt;a href="http://www.sharpcrafters.com/" target="_blank"&gt;PostSharp&lt;/a&gt; and how to use it to do cool stuff from simple logging to lazy loading and adding attributes and interfaces at compile time.&lt;br /&gt;
I won’t post the slides for the second session most of it was done in Visual Studio. All of the code I’ve shown &lt;a href="https://skydrive.live.com/redir.aspx?cid=64d349aba764a780&amp;amp;resid=64D349ABA764A780!285&amp;amp;parid=64D349ABA764A780!284&amp;amp;authkey=!AGfPXpkmo6o60-E" target="_blank"&gt;can be downloaded here&lt;/a&gt;.&lt;br /&gt;
The examples are also available here.&lt;br /&gt;
I had a great audience – they participated and asked a lot of questions most of which I managed to answer.&lt;br /&gt;
&lt;br /&gt;
All in all a good day…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=romGOZFqn6Y:1OhxqQtjpVw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=romGOZFqn6Y:1OhxqQtjpVw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=romGOZFqn6Y:1OhxqQtjpVw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=romGOZFqn6Y:1OhxqQtjpVw:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=romGOZFqn6Y:1OhxqQtjpVw:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=romGOZFqn6Y:1OhxqQtjpVw:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=romGOZFqn6Y:1OhxqQtjpVw:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=romGOZFqn6Y:1OhxqQtjpVw:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/romGOZFqn6Y" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/romGOZFqn6Y/aop-sessions-on-idndug.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/03/aop-sessions-on-idndug.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-7544989649917769223</guid><pubDate>Wed, 07 Mar 2012 19:16:00 +0000</pubDate><atom:updated>2012-03-07T21:16:17.724+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">.NET</category><category domain="http://www.blogger.com/atom/ns#">Unit tests</category><category domain="http://www.blogger.com/atom/ns#">NUnit</category><category domain="http://www.blogger.com/atom/ns#">AOP</category><title>NUnit’s new Action Attributes is AOP to your unit tests</title><description>&lt;p&gt;With the new &lt;a href="http://nunit.org/index.php?p=docHome&amp;amp;r=2.6" target="_blank"&gt;NUnit release (v2.6)&lt;/a&gt; introduce a new feature called &lt;a href="http://nunit.org/index.php?p=actionAttributes&amp;amp;r=2.6" target="_blank"&gt;Action Attributes&lt;/a&gt; which means that now NUnit has rolled out it’s own mini-AOP capabilities.&lt;/p&gt;  &lt;p&gt;In the past &lt;em&gt;SetUp&lt;/em&gt; and &lt;em&gt;TearDown&lt;/em&gt; where used to perform actions before and/or after a test run, they worked well enough but were limited to running certain operation only on the tests on that test class – on all thee tests. Confronted with the need to perform the same operation for multiple test classes the test writer (you) needed to resort to inheritance which is not bad solution as long as you do not need to support multiple &amp;quot;pre/post behaviors on some of the tests. &lt;/p&gt;  &lt;p&gt;And there was always the solution of writing the same code over and over in all of the tests either as duplicating the code or putting it inside a method and calling it from all the tests. Sounds familiar? To me it sounds just like a case of a simple cross-cutting concern.&lt;/p&gt;  &lt;h3&gt;How it works&lt;/h3&gt;  &lt;p&gt;All you have to to is implement the following interface:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;public interface ITestAction&lt;br /&gt;{&lt;br /&gt;    void BeforeTest(TestDetails details);&lt;br /&gt;&lt;br /&gt;    void AfterTest(TestDetails details);&lt;br /&gt;&lt;br /&gt;    ActionTargets Targets { get; }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;and that’s it! For more details – head to &lt;a href="http://www.nunit.org/index.php?p=actionAttributes&amp;amp;r=2.6" target="_blank"&gt;NUnit’s site for full description&lt;/a&gt;.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now adding Aspects is as simple as creating a class and then putting the attribute on a method, class or even assembly:&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush: csharp;"&gt;[TestFixture, ResetServiceLocator]&lt;br /&gt;public class MyTests&lt;br /&gt;{&lt;br /&gt;    [Test, CreateTestDatabase]&lt;br /&gt;    public void Test1() { /* ... */ }&lt;br /&gt;&lt;br /&gt;    [Test, CreateTestDatabase, AsAdministratorPrincipal]&lt;br /&gt;    public void Test2() { /* ... */ }&lt;br /&gt;&lt;br /&gt;    [Test, CreateTestDatabase, AsNamedPrincipal(&amp;quot;charlie.poole&amp;quot;)]&lt;br /&gt;    public void Test3() { /* ... */ }&lt;br /&gt;&lt;br /&gt;    [Test, AsGuestPrincipal]&lt;br /&gt;    public void Test4() { /* ... */ }&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Any issues?&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The only downside is the being a new feature it’s only supported by NUnit’s GUI and console runners. This means that if you’re using a 3rd party runner such as TesDriven.NET, R# or CodeRush you’ll have to wait until support is added in some future release.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;In the past other testing frameworks such as MBUnit (and even MSTest) had similar features – but never this simple.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;There you have it – simple unit testing AOP curtsy of NUnit. Now that I have it I don’t understand how I never felt that this functionality was missing. The new Action Attributes really shine in integration tests. Making database setup, running under specific permissions or just making sure a server is up before running the tests.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&amp;#160;&lt;/p&gt;&lt;br /&gt;Happy Coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=CVjCXqiYzCo:w5tIjbfskL4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=CVjCXqiYzCo:w5tIjbfskL4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=CVjCXqiYzCo:w5tIjbfskL4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=CVjCXqiYzCo:w5tIjbfskL4:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=CVjCXqiYzCo:w5tIjbfskL4:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=CVjCXqiYzCo:w5tIjbfskL4:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=CVjCXqiYzCo:w5tIjbfskL4:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=CVjCXqiYzCo:w5tIjbfskL4:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/CVjCXqiYzCo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/CVjCXqiYzCo/nunits-new-action-attributes-is-aop-to.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>2</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/03/nunits-new-action-attributes-is-aop-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-3892307521412602220</guid><pubDate>Wed, 29 Feb 2012 20:30:00 +0000</pubDate><atom:updated>2012-02-29T22:30:26.013+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Typemock</category><category domain="http://www.blogger.com/atom/ns#">PostSharp</category><category domain="http://www.blogger.com/atom/ns#">Tools</category><category domain="http://www.blogger.com/atom/ns#">Open Source</category><title>Software news – February 2012</title><description>&lt;p&gt;It’s been a while since last I’ve written about new releases and features in software I like so I’ve decided to shine a light on three software development tools I love, two exiting beta releases and one hidden feature brought into the light:&lt;/p&gt;  &lt;h4&gt;SharpDevelop 4.2 Beta&lt;/h4&gt;  &lt;p&gt;The open source, light free .NET IDE has just announced another &lt;a href="http://community.sharpdevelop.net/forums/t/14472.aspx" target="_blank"&gt;beta release&lt;/a&gt;. I’ve been following &lt;a href="http://www.icsharpcode.net/opensource/sd/" target="_blank"&gt;#Develop&lt;/a&gt; since it was created in the midst of .NET 1 and it just keeps on getting better. This release has a few cool features such as ASP.NET MVC 3 support and &lt;a href="http://community.sharpdevelop.net/blogs/mattward/archive/2011/11/09/MachineSpecifications.aspx" target="_blank"&gt;MSpec&lt;/a&gt; support – with an integrated test runner.&lt;/p&gt;  &lt;p&gt;&lt;img alt="MSpec behaviours in Unit Tests window" src="http://community.sharpdevelop.net/photos/mattward/images/original/MSpecBehavioursInUnitTestsWindow.aspx" /&gt;&lt;/p&gt;  &lt;p&gt;The good people of #Develop have added an additional static analysis tool - the &lt;a href="http://community.sharpdevelop.net/blogs/siegfried_pammer/archive/2012/02/06/sharpdevelop-4-2-code-quality-analysis-dependency-matrix.aspx" target="_blank"&gt;dependency metrix&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;&lt;img src="http://community.sharpdevelop.net/photos/siegfriedpammer/images/38475/624x480.aspx" /&gt;&lt;/p&gt;  &lt;p&gt;It always amaze me on how this tool finds new way to innovate and I hope that the Visual Studio team look at this tool from time to time to see what is still missing from Visual Studio.&lt;/p&gt;  &lt;h3&gt;PostSharp Toolkits&lt;/h3&gt;  &lt;p&gt;If you don’t know what &lt;a href="http://www.sharpcrafters.com/" target="_blank"&gt;PostSharp&lt;/a&gt; or AOP is – it’s not too late to learn. In a nutshell PostSharp utilize attributes to add &lt;a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" target="_blank"&gt;cross cutting concerns&lt;/a&gt; into existing .NET code. But for those of you that don’t like to see additional attributes on their code there is a new tool on the block. Using &lt;a href="http://nuget.codeplex.com/" target="_blank"&gt;NuGet&lt;/a&gt; you can add logging to every function on the code with a click of a button. And the great thing is that it’s &lt;a href="https://github.com/sharpcrafters/PostSharp-Toolkits" target="_blank"&gt;open source&lt;/a&gt;, I plan to dig into the code and find out if I can use the power of the PostSharp SDK…&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-fTkcRUjNaqU/T06K0eCb-pI/AAAAAAAADAQ/RFe50J74Lng/s1600-h/image%25255B5%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh6.ggpht.com/-BYyMzg7EHfo/T06K2-7YJxI/AAAAAAAADAY/fuRA9RVMpLg/image_thumb%25255B3%25255D.png?imgmax=800" width="644" height="431" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;If you want to know more about it - &lt;a href="http://www.hmemcpy.com/blog/" target="_blank"&gt;Igal&lt;/a&gt; has written all about it on the &lt;a href="http://www.sharpcrafters.com/blog/post/introducing-postsharp-toolkits.aspx" target="_blank"&gt;SharpCrafters blog&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;Isolator 7 Beta&lt;/h3&gt;  &lt;p&gt;Disclaimer – I used to work at &lt;a href="www.typemock.com" target="_blank"&gt;Typemock&lt;/a&gt;, regardless I believe that the next Isolator release is going to make some waves. In the next version (V7) &lt;a href="http://www.typemock.com/isolator-v7" target="_blank"&gt;Typemock Isolator&lt;/a&gt; becomes more than a “mocking tool”. The new Autorunner feature that runs all of the unit tests in the background while checking code coverage is just what I need in my team with the 3000+ tests we have. But that’s not all – &lt;a href="www.typemock.com" target="_blank"&gt;Typemock&lt;/a&gt; has taken the idea of &lt;a href="http://www.google.co.il/search?sourceid=chrome&amp;amp;ie=UTF-8&amp;amp;q=uto+mocking+container" target="_blank"&gt;auto-mocking containers&lt;/a&gt; to the next level with the ability to create a fake instance and change the behavior of its dependencies after creation – it’s quite a lot to explain here, I’ll have to write a post just for this feature.&lt;/p&gt;  &lt;p&gt;So if it’s sounds interesting – check out &lt;a href="http://www.typemock.com/isolator-v7-preview" target="_blank"&gt;this webcast&lt;/a&gt; or better yet head to &lt;a href="http://www.typemock.com/isolator-v7" target="_blank"&gt;the beta sign-up page&lt;/a&gt; and see for yourself. &lt;/p&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=98nPGGKL4xM:YK33yrrm5jE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=98nPGGKL4xM:YK33yrrm5jE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=98nPGGKL4xM:YK33yrrm5jE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=98nPGGKL4xM:YK33yrrm5jE:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=98nPGGKL4xM:YK33yrrm5jE:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=98nPGGKL4xM:YK33yrrm5jE:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=98nPGGKL4xM:YK33yrrm5jE:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=98nPGGKL4xM:YK33yrrm5jE:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/98nPGGKL4xM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/98nPGGKL4xM/software-news-february-2012.html</link><author>noreply@blogger.com (Dror Helper)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-BYyMzg7EHfo/T06K2-7YJxI/AAAAAAAADAY/fuRA9RVMpLg/s72-c/image_thumb%25255B3%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/02/software-news-february-2012.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-8412831559140848595.post-6426505831009755642</guid><pubDate>Wed, 15 Feb 2012 20:50:00 +0000</pubDate><atom:updated>2012-02-15T22:54:38.008+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Unit tests</category><category domain="http://www.blogger.com/atom/ns#">Thoughts</category><category domain="http://www.blogger.com/atom/ns#">TDD</category><title>Why you need to make your tests fail</title><description>Test Driven Development (TDD) have many benefits. For start it’s a design methodology that help avoiding “&lt;a href="http://en.wikipedia.org/wiki/Analysis_paralysis" target="_blank"&gt;Analysis paralysis&lt;/a&gt;” and make sure that you only have the needed code to solve a problem.&lt;br /&gt;
Yesterday I found another benefit of writing the tests before the code – you get to see them fail!&lt;br /&gt;
A while back I wrote about another shortcoming of MSTest – how it does not have any built in facility to &lt;a href="http://blog.drorhelper.com/2009/08/checking-expected-exception-message.html" target="_blank"&gt;test against expected exception message&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Unknown to me at the time there was a bug hidden in the code I’ve published – see if you can spot it:
&lt;br /&gt;
&lt;pre class="brush: csharp;"&gt;public static class MyAssert
{
    public static void BadThrows;&amp;lt;T&amp;gt;(Action action) where T : Exception
    {
        try
        {
            action();

            Assert.Fail("Exception of type {0} should be thrown.", typeof(T));
        }
        catch (T exception)
        {
     // All is well!
        }
    }
}&lt;/pre&gt;
BTW this example was simplified for your viewing pleasure - I want to make a point here, not stick to the facts. have you found the bug yet? &lt;br /&gt;
&lt;br /&gt;
Consider the following test:&lt;br /&gt;
&lt;pre class="brush: csharp;"&gt;[TestMethod]
public void ThisTestWouldNeverFail()
{
    MyAssert.BadThrows&amp;lt;exception&amp;gt;(() =&amp;gt; Dummy.SomeMethod());
}&lt;/pre&gt;
Have you guessed it? (hint: its in the test name) &lt;br /&gt;
&lt;br /&gt;
That's right! this test would never have failed. If we take a closer look at &lt;em&gt;BadThrows&lt;/em&gt; we’ll see the problem.&lt;br /&gt;
&lt;br /&gt;
MSTest just like any other .NET unit testing framework throws an exception when assertions fails. In this case because we’re expecting an exception of type &lt;em&gt;Exception&lt;/em&gt; any other exception thrown including the AssertFailedException would be caught – causing the test to pass.&lt;br /&gt;
&lt;br /&gt;
That’s right – because the user expects an exception of type”Exception” to be thrown plus a minor bug the test is completely useless – if you want to see the working code just &lt;a href="http://blog.drorhelper.com/2009/08/checking-expected-exception-message.html" target="_blank"&gt;head to this post&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I did not find this bug mainly because I don’t like to throw and assert for &lt;em&gt;Exception&lt;/em&gt; seems like a bad practice and in retrospective I should have avoided my co-workers from doing so by throwing exception of my own in case they try to do it.&lt;br /&gt;
&lt;br /&gt;
The guy that did find this bug found it because he wrote a test and wanted to see it fail. That’s right it wasn’t enough for him to know that his code made the test pass – he had to make sure that it failed first – some people…&lt;br /&gt;
A quick search in our code found multiple tests that did exactly that which means two things:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;These test were not written using TDD&lt;/li&gt;
&lt;li&gt;The authors did not even bother to see the tests fail&lt;/li&gt;
&lt;/ol&gt;
After fixing the bug we found one test that started failing – which means that not only was the test wrong – so was the code it was testing.&lt;br /&gt;
&lt;br /&gt;
I’ve talked to the team, explained why throwing “Exception” is not a best practice and why tests need to be “tested” by seeing them fail and we’ve fixed the tests and the code.&lt;br /&gt;
&lt;br /&gt;
I think I learnt quite a lot from this experience, I know now who does TDD and who doesn’t and I found a new problem that occurs when developers write their tests after the code.&lt;br /&gt;
&lt;br /&gt;
Happy coding…&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=E4uBhWKGZpc:2_J_Fu9CbeQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=E4uBhWKGZpc:2_J_Fu9CbeQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=E4uBhWKGZpc:2_J_Fu9CbeQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=E4uBhWKGZpc:2_J_Fu9CbeQ:zYSYRoQSaQY"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=zYSYRoQSaQY" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=E4uBhWKGZpc:2_J_Fu9CbeQ:I9og5sOYxJI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=I9og5sOYxJI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=E4uBhWKGZpc:2_J_Fu9CbeQ:VWBAqf-qjwI"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?d=VWBAqf-qjwI" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/HelperCode?a=E4uBhWKGZpc:2_J_Fu9CbeQ:ihZm8jKPjHg"&gt;&lt;img src="http://feeds.feedburner.com/~ff/HelperCode?i=E4uBhWKGZpc:2_J_Fu9CbeQ:ihZm8jKPjHg" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/HelperCode/~4/E4uBhWKGZpc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/HelperCode/~3/E4uBhWKGZpc/why-you-need-to-make-your-tests-fail.html</link><author>noreply@blogger.com (Dror Helper)</author><thr:total>1</thr:total><gd:extendedProperty name="commentSource" value="1" /><gd:extendedProperty name="commentModerationMode" value="FILTERED_POSTMOD" /><feedburner:origLink>http://blog.drorhelper.com/2012/02/why-you-need-to-make-your-tests-fail.html</feedburner:origLink></item></channel></rss>
