<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Over It. &#187; Uncategorized</title>
	<atom:link href="http://jason.diamond.name/weblog/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://jason.diamond.name/weblog</link>
	<description>(a Weblog by Jason Diamond)</description>
	<lastBuildDate>Mon, 30 Jan 2012 20:08:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>WebSocket Webinar Demo Code</title>
		<link>http://jason.diamond.name/weblog/2012/01/30/websocket-webinar-demo-code/</link>
		<comments>http://jason.diamond.name/weblog/2012/01/30/websocket-webinar-demo-code/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 20:08:01 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[WebSocket]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=175</guid>
		<description><![CDATA[Thanks to those who attended today&#8217;s webinar. The demo code is located here until it (and the recording) get uploaded to the DM website later this week. Feel free to email with questions!]]></description>
			<content:encoded><![CDATA[<p>Thanks to those who attended today&#8217;s webinar. The demo code is located <a href="http://dl.dropbox.com/u/4389817/WebSocketsDemo.zip">here</a> until it (and the recording) get uploaded to the DM website later this week.</p>
<p>Feel free to email with questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2012/01/30/websocket-webinar-demo-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Webinar</title>
		<link>http://jason.diamond.name/weblog/2011/08/04/jquery-webinar/</link>
		<comments>http://jason.diamond.name/weblog/2011/08/04/jquery-webinar/#comments</comments>
		<pubDate>Thu, 04 Aug 2011 21:18:31 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[DevelopMentor]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=173</guid>
		<description><![CDATA[I&#8217;ll be doing a webinar for DevelopMentor tomorrow at 11 AM Pacific. The title is &#8220;Spice Up Your Web Applications with a Dash of jQuery!&#8221;. I&#8217;ll be enhancing a simple MVC application so that it &#8220;feels&#8221; a bit slicker for your users. If you know JavaScript and jQuery, it&#8217;s basic stuff, but the goal is [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be doing a webinar for <a href="http://www.develop.com/">DevelopMentor</a> tomorrow at 11 AM Pacific. The title is &#8220;Spice Up Your Web Applications with a Dash of jQuery!&#8221;.</p>
<p>I&#8217;ll be enhancing a simple MVC application so that it &#8220;feels&#8221; a bit slicker for your users. If you know JavaScript and jQuery, it&#8217;s basic stuff, but the goal is really to help those who aren&#8217;t as comfortable doing that to become confident enough to dive in and start learning.</p>
<p>I&#8217;ve been told that more people registered than we can actually have join. Sorry in advance if anybody can&#8217;t get in. You&#8217;ll be able to watch a recording of it afterwards <a href="http://www.develop.com/webcasts">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2011/08/04/jquery-webinar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Even More Useful Fiddler Bookmarklet</title>
		<link>http://jason.diamond.name/weblog/2011/03/29/an-even-more-useful-fiddler-bookmarklet/</link>
		<comments>http://jason.diamond.name/weblog/2011/03/29/an-even-more-useful-fiddler-bookmarklet/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 20:35:30 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Fiddler]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=169</guid>
		<description><![CDATA[It&#8217;s actually the same bookmarklet as the one I wrote about before, but this time it&#8217;s in the form of a link that makes it easier to add to your browser: fiddler-ify]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s actually the same bookmarklet as the one I wrote about <a href="http://jason.diamond.name/weblog/2010/01/13/a-useful-fiddler-bookmarklet/">before</a>, but this time it&#8217;s in the form of a link that makes it easier to add to your browser:</p>
<p><a href="javascript:(function(){window.location=window.location.toString().replace('localhost', 'ipv4.fiddler');})()">fiddler-ify</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2011/03/29/an-even-more-useful-fiddler-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Deferreds</title>
		<link>http://jason.diamond.name/weblog/2011/03/28/jquery-deferreds/</link>
		<comments>http://jason.diamond.name/weblog/2011/03/28/jquery-deferreds/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 23:55:37 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=163</guid>
		<description><![CDATA[I’m absolutely loving the new Deferred objects in jQuery 1.5 and am using them heavily in a new project at work. I just spent a bit of time trying to figure out why they weren’t working the way I was expecting them to, actually having to step into the jQuery code to realize my mistake. [...]]]></description>
			<content:encoded><![CDATA[<p>I’m absolutely loving the new <a href="http://api.jquery.com/category/deferred-object/">Deferred objects</a> in jQuery 1.5 and am using them heavily in a new project at work.</p>
<p>I just spent a bit of time trying to figure out why they weren’t working the way I was expecting them to, actually having to step into the jQuery code to realize my mistake.</p>
<p>The <code>$.when</code> method can take in one or more Deferred objects, but it can&#8217;t take them in as an array. Once I realized this, I immediately realized this could be solved with JavaScript’s <code>apply</code> function like this:</p>
<pre class="brush:js">$.when.apply($, myDeferreds).done(function () {
    // All deferreds were resolved...
});</pre>
<p>The first argument to <code>apply</code> is the object that gets to be “this” inside the <code>when</code> method. The second argument is the array containing the deferreds.</p>
<p>If <code>myDeferreds</code> contains three deferred objects, the above is the equivalent of this:</p>
<pre class="brush:js">$.when(deferred1, deferred2, deferred3).done(function () {
    // All deferreds were resolved...
});</pre>
<p>I thought that it would be neat if <code>$.when</code> would check to see if it was passed an array, but the jQuery team <a href="http://bugs.jquery.com/ticket/8256">disagrees</a>. Here&#8217;s hoping I won&#8217;t make this mistake again&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2011/03/28/jquery-deferreds/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Asserting without Equals</title>
		<link>http://jason.diamond.name/weblog/2010/10/30/asserting-without-equals/</link>
		<comments>http://jason.diamond.name/weblog/2010/10/30/asserting-without-equals/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 18:09:53 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=153</guid>
		<description><![CDATA[Arnis suggested that implementing Equals just for NUnit was wrong so I thought I&#8217;d try doing without it. The CollectionAssert.AreEqual method accepts an optional IComparer implementation. If specified, that will be used instead of Equals. So I put together a class called PartComparer. Since I switched to comparing the state of the objects outside their [...]]]></description>
			<content:encoded><![CDATA[<p>Arnis suggested that implementing <code>Equals</code> just for NUnit was wrong so I thought I&#8217;d try doing without it.</p>
<p>The <code>CollectionAssert.AreEqual</code> method accepts an optional <code>IComparer</code> implementation. If specified, that will be used instead of <code>Equals</code>.</p>
<p>So I put together a class called <code>PartComparer</code>. Since I switched to comparing the state of the objects outside their classes, I had to expose some of that state via read-only properties. I think I can live with that.</p>
<p>I then deleted all of my <code>Equals</code> and <code>GetHashCode</code> methods (I wasn&#8217;t really using <code>GetHashCode</code> anyways).</p>
<p>Here&#8217;s what the test changed to:</p>
<pre class="brush:csharp">[Test]
public void It_scans_literal_text()
{
    var scanner = new Scanner();

    var parts = scanner.Scan("foo");

    CollectionAssert.AreEqual(new Part[]
                                  {
                                      new LiteralText("foo"),
                                  },
                              parts,
                              new PartComparer());
}</pre>
<p>It works the same as before. It just requires the extra argument. </p>
<p>Custom comparers works even with the newer <code>Assert.That</code> syntax:</p>
<pre class="brush:csharp">[Test]
public void It_scans_literal_text()
{
    var scanner = new Scanner();

    var parts = scanner.Scan("foo");

    Assert.That(parts, Is.EqualTo(new Part[]
                                      {
                                          new LiteralText("foo"),
                                      })
                         .Using(new PartComparer()));
}</pre>
<p>The verbosity of constructing the expected <code>Part</code> array is a bit much. If only C# could construct lists like JavaScript, Python, and Ruby&#8230;</p>
<p>I decided to try to hide that in a custom assertion method:</p>
<pre class="brush:csharp">private static void AssertThatPartsAreEqual(
    IEnumerable&lt;Part> actualParts,
    params Part[] expectedParts)
{
    Assert.That(actualParts, Is.EqualTo(expectedParts)
                               .Using(new PartComparer()));
}</pre>
<p>Now my test looks like this:</p>
<pre class="brush:csharp">[Test]
public void It_scans_literal_text()
{
    var scanner = new Scanner();

    var parts = scanner.Scan("foo");

    AssertThatPartsAreEqual(parts, new LiteralText("foo"));
}</pre>
<p>One really nice thing about having the custom assertion method is that I can modify how parts are compared in just one spot. For example, <code>Scan</code> is really an iterator. NUnit&#8217;s failure messages when the collections aren&#8217;t arrays are less than ideal. With the comparisons being done in just one spot, I can modify it to convert the enumerable into an array:</p>
<pre class="brush:csharp">private static void AssertThatPartsAreEqual(
    IEnumerable&lt;Part> actualParts,
    params Part[] expectedParts)
{
    Assert.That(actualParts.ToArray(), Is.EqualTo(expectedParts)
                                         .Using(new PartComparer()));
}</pre>
<p>Since I&#8217;m using .NET 3.5, I can take this one step further and use an extension method:</p>
<pre class="brush:csharp">internal static class EnumerablePartExtensions
{
    public static void IsEqualTo(
        this IEnumerable&lt;Part> actualParts,
        params Part[] expectedParts)
    {
        Assert.That(actualParts.ToArray(), Is.EqualTo(expectedParts)
                                             .Using(new PartComparer()));
    }
}</pre>
<p>With that in place, my test now looks like this:</p>
<pre class="brush:csharp">[Test]
public void It_scans_literal_text()
{
    var scanner = new Scanner();

    var parts = scanner.Scan("foo");

    parts.IsEqualTo(new LiteralText("foo"));
}</pre>
<p>Wow, it&#8217;s like Ruby but without the monkey patching!</p>
<p>One thing I did leave in my code are all of the <code>ToString</code> overrides. Without those, NUnit&#8217;s failure messages would be much less helpful. They&#8217;re also very useful while debugging.</p>
<p>Thanks, Arnis. Your comment helped me find an alternative (and quite possibly better!) way to get what I want.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/10/30/asserting-without-equals/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>I fail at TDD?</title>
		<link>http://jason.diamond.name/weblog/2010/10/29/i-fail-at-tdd/</link>
		<comments>http://jason.diamond.name/weblog/2010/10/29/i-fail-at-tdd/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 21:57:51 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=129</guid>
		<description><![CDATA[I actually think I&#8217;m pretty good at TDD. Every now and then I get reminded that I&#8217;m not as good as I think I am. I&#8217;ve been working on a new project (an implementation of the Mustache template language in C# that I&#8217;m calling Nustache) and have been having a lot of fun with it. [...]]]></description>
			<content:encoded><![CDATA[<p>I actually think I&#8217;m pretty good at TDD. Every now and then I get reminded that I&#8217;m not as good as I think I am.</p>
<p>I&#8217;ve been working on a new project (an implementation of the <a href="http://mustache.github.com/">Mustache</a> template language in C# that I&#8217;m calling <a href="http://github.com/jdiamond/Nustache">Nustache</a>) and have been having a lot of fun with it. This is the project I&#8217;m going to use as an example of how I fail at TDD.</p>
<p>Since this project involves parsing a string, I decided I would probably need a class to scan the string for tokens so those tokens could be parsed and then evaluated.</p>
<p>While writing the test for my <code>Scanner</code> class, I wrote it so that it would assert on the sequence of tokens it returns. I decided the tokens would be instances of a class called <code>Part</code>. One specific subclass of <code>Part</code> would be <code>LiteralText</code>. It represents a span of characters from the source template that is <em>not</em> supposed to be evaluated and just rendered directly to the output. I figured this would be the easiest way to start testing my <code>Scanner</code> class.</p>
<p>The test probably looked like this (I&#8217;m writing this way after the fact):</p>
<pre class="brush:csharp">[Test]
public void It_scans_literal_text()
{
    var scanner = new Scanner();

    var parts = scanner.Scan("foo");

    CollectionAssert.AreEqual(new Part[]
                              {
                                  new LiteralText("foo"),
                              },
                              parts);
}</pre>
<p>At this point, the test didn&#8217;t compile because I hadn&#8217;t defined my <code>Scanner</code>, <code>Part</code>, and <code>LiteralText</code> classes yet.</p>
<p>Having written the test first, I learned a few things about the <code>Scanner</code> class it was trying to test:</p>
<ul>
<li>It has a default constructor</li>
<li>It has a method named <code>Scan</code></li>
<li>Its <code>Scan</code> method takes in a string</li>
<li>Its <code>Scan</code> method returns an <code>IEnumerable&lt;Part></code> (I know this because of the parameters for <code>CollectionAssert.AreEqual</code>)</li>
</ul>
<p>I also learned something about the <code>LiteralText</code> class:</p>
<ul>
<li>It derives from <code>Part</code> (because I&#8217;m adding it to a <code>Part</code> array)</li>
<li>It has a constructor that accepts a string</li>
<li>It must override the <code>Equals</code> method or this will never work</li>
</ul>
<p>Since this test is describing the <code>Scanner</code> class, I decided to work on it first:</p>
<pre class="brush:csharp">public class Scanner
{
    public IEnumerable&lt;Part> Scan(string template)
    {
       return null;
    }
}</pre>
<p>This wouldn&#8217;t compile until I defined <code>Part</code>:</p>
<pre class="brush:csharp">public class Part
{
}</pre>
<p>The test still needed <code>LiteralText</code> to be defined:</p>
<pre class="brush:csharp">public class LiteralText : Part
{
    public LiteralText(string text)
    {
    }
}</pre>
<p>At this point, I was able to compile and run my test. When I did, NUnit said this:</p>
<pre>Test 'Nustache.Tests.Describe_Scanner_Scan.It_scans_literal_text' failed:
  Expected: &lt; &lt;Nustache.Core.LiteralText> >
  But was:  null</pre>
<p>I liked that failure message, but I wanted to go a bit further and see what the failure message would be when I return an empty array instead of <code>null</code> since it didn&#8217;t make sense to me for <code>Scan</code> to return <code>null</code>. <code>Scan</code> changed to this:</p>
<pre class="brush:csharp">public IEnumerable&lt;Part> Scan(string template)
{
    return new Part[] { };
}</pre>
<p>The failure message became this:</p>
<pre>Test 'Nustache.Tests.Describe_Scanner_Scan.It_scans_literal_text' failed:
  Expected is &lt;Nustache.Core.Part[1]>, actual is &lt;Nustache.Core.Part[0]>
  Values differ at index [0]
  Missing:  &lt; &lt;Nustache.Core.LiteralText> >
</pre>
<p>OK, that wasn&#8217;t too bad. Next, I wanted to see if I could get it to pass by doing the simplest thing I could possibly do so I changed <code>Scan</code> to this:</p>
<pre class="brush:csharp">public IEnumerable<Part> Scan(string template)
{
    return new Part[]
               {
                   new LiteralText("foo")
               };
}</pre>
<p>After seeing that pass, I would have implemented it a little more realistically, but I was in for a surprise. Instead of passing (which is what I expected), I got this failure message:</p>
<pre>Test 'Nustache.Tests.Describe_Scanner_Scan.It_scans_literal_text' failed:
  Expected and actual are both &lt;Nustache.Core.Part[1]>
  Values differ at index [0]
  Expected: &lt;Nustache.Core.LiteralText>
  But was:  &lt;Nustache.Core.LiteralText></pre>
<p>Uh&#8230; Oh, yeah! <code>LiteralText</code> needs an override of the <code>Equals</code> method or NUnit will never be able to tell if one instance is &#8220;equal to&#8221; another.</p>
<p>In order to implement that, I need to make sure the string that gets passed in to the <code>LiteralText</code> constructor gets saved in some sort of field or property. Then I could write my <code>Equals</code> override by hand or let ReSharper generate it for me.</p>
<p>I decided to let ReSharper do it (I&#8217;m lazy) and got three methods: <code>bool Equals(LiteralText other)</code>, <code>bool Equals(object obj)</code>, and <code>int GetHashCode()</code>.</p>
<p>After getting that to work, I added a <code>ToString</code> method to <code>LiteralText</code> to make the failure message even clearer.</p>
<p>See the problem? I went off and started implementing code in <code>LiteralText</code> when I was in the middle of trying to get a test for <code>Scanner</code> to pass! Sure, it&#8217;s <em>just</em> the <code>Equals</code> and <code>GetHashCode</code> methods, but it&#8217;s still code!</p>
<p>I did all of this in response to test I was trying to get to pass so I was still doing TDD, right?</p>
<p>Right?</p>
<p>At the time I was doing this, I didn&#8217;t even notice this &#8220;problem&#8221;. It wasn&#8217;t until much later when I decided to run my tests under NCover to see how I was doing. I was practicing TDD, so my coverage should have been pretty good, if not perfect. Sadly, I found I had a bunch of <code>Equals</code>, <code>GetHashCode</code>, and <code>ToString</code> methods that weren&#8217;t fully covered and ruining my flawless code coverage report!</p>
<p>So what&#8217;s the big deal? Everybody agrees that 100% code coverage isn&#8217;t sufficient to ensure the correctness of your code. I absolutely agree with that. Many people also agree that getting 100% code coverage isn&#8217;t even worth it. That, I disagree with. As does Patrick Smacchia (author of <a href="http://www.ndepend.com/">NDepend</a>) who described why 100% code coverage is a worthwhile goal <a href="http://codebetter.com/blogs/patricksmacchia/archive/2009/06/07/high-test-coverage-ratio-is-a-good-thing-anyway.aspx">here</a>. It&#8217;s a great article and I highly recommend you all read it.</p>
<p>To rectify this predicament, I forced myself to write tests for my <code>LiteralText</code> class (writing tests after the fact is so boring!).</p>
<p>Since I originally defined it, I discovered that <code>Part</code> had grown a <code>Render</code> method and <code>LiteralText</code> was overriding it. The method was being covered by other tests, but there was nothing that was <em>directly</em> testing <code>LiteralText</code>. That might not be such a big deal, but one of the oft-touted benefits of unit tests is that they can also act as <em>executable documentation</em>. Since I had no unit tests for my <code>LiteralText</code> class, I had no executable documentation for it! How would I ever re-learn (months from now) how it&#8217;s supposed to behave without that?</p>
<p>OK, I&#8217;m being a bit silly, but I went for it anyways and I really liked the result. Here&#8217;s what I came up with:</p>
<pre class="brush:csharp">[TestFixture]
public class Describe_LiteralText
{
    [Test]
    public void It_cant_be_constructed_with_null_text()
    {
        Assert.Throws&lt;ArgumentNullException>(() => new LiteralText(null));
    }

    [Test]
    public void It_renders_its_text()
    {
        var a = new LiteralText("a");
        var writer = new StringWriter();
        var context = new RenderContext(null, null, writer, null);

        a.Render(context);

        Assert.AreEqual("a", writer.GetStringBuilder().ToString());
    }

    [Test]
    public void It_has_a_useful_Equals_method()
    {
        object a = new LiteralText("a");
        object a2 = new LiteralText("a");
        object b = new LiteralText("b");

        Assert.IsTrue(a.Equals(a));
        Assert.IsTrue(a.Equals(a2));
        Assert.IsTrue(a2.Equals(a));
        Assert.IsFalse(a.Equals(b));
        Assert.IsFalse(a.Equals(null));
        Assert.IsFalse(a.Equals("a"));
    }

    [Test]
    public void It_has_an_Equals_overload_for_other_LiteralText_objects()
    {
        var a = new LiteralText("a");
        var a2 = new LiteralText("a");
        var b = new LiteralText("b");

        Assert.IsTrue(a.Equals(a));
        Assert.IsTrue(a.Equals(a2));
        Assert.IsTrue(a2.Equals(a));
        Assert.IsFalse(a.Equals(b));
        Assert.IsFalse(b.Equals(a));
        Assert.IsFalse(a.Equals(null));
    }

    [Test]
    public void It_has_a_useful_GetHashCode_method()
    {
        var a = new LiteralText("a");

        Assert.AreNotEqual(0, a.GetHashCode());
    }

    [Test]
    public void It_has_a_useful_ToString_method()
    {
        var a = new LiteralText("a");

        Assert.AreEqual("LiteralText(\"a\")", a.ToString());
    }
}</pre>
<p>As you can probably tell, I&#8217;m using a non-standard (for .NET developers) naming scheme for my tests. It&#8217;s inspired by <a href="http://rspec.info/">RSpec</a> and I really like it.</p>
<p>If you take away all the code, the ugly underscores, and the weird prefixes, you get the documentation:</p>
<ul>
<li>LiteralText</li>
<ul>
<li>can&#8217;t be constructed with null text</li>
<li>renders its text</li>
<li>has a useful Equals method</li>
<li>has an Equals overload for other LiteralText objects</li>
<li>has a useful GetHashCode method</li>
<li>has a useful ToString method</li>
</ul>
</ul>
<p>Could that get any clearer? (Seriously, leave a comment if you think it could.)</p>
<p>I formatted that list by hand, but generating it could easily be automated by processing NUnit&#8217;s XML output or using reflection on the test assembly. RSpec has a feature built in to it that can generate this kind of output. (Don&#8217;t worry, I don&#8217;t plan on switching to Ruby like every other .NET weblogger out there seems to be doing. I do think they have some great ideas, though, and have been really enjoying reading the beta version of the <a href="http://www.pragprog.com/titles/achbd/the-rspec-book">RSpec Book</a> which is what made me try out this new naming scheme.)</p>
<p>Even though this particular class is trivial, doing this helped set up an example to follow for the other <code>Part</code> subclasses which aren&#8217;t as simple. Also, I feel much more confident about this class knowing that there is a suite of tests in a single, well-named fixture that provides 100% code coverage for it.</p>
<p>I&#8217;m not saying that every class should be covered by one and only one fixture. If the class demands it, I&#8217;ll happily break its tests up into multiple fixtures. I could have one fixture per method or one fixture per context. I&#8217;m flexible about that. I&#8217;d use the desire to that as a possible smell that the class might be trying to do too much, though.</p>
<p>You can also see that I&#8217;m pretty flexible about not limiting myself to just <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=35578">one assertion per test</a>. I strongly believe that most tests should only have one assertion but, in this case, it would have been ridiculous to have written a test case for each of the different ways <code>Equals</code> could be invoked.</p>
<p>I was also a little lax on the <a href="http://c2.com/cgi/wiki?ArrangeActAssert">Arrange/Act/Assert</a> format. This is another practice that I try to consistently follow. Some tests just don&#8217;t need anything to be set up! And NUnit&#8217;s <code>Assert.Throws</code> syntax kind of forces you to act and assert at the same time. There&#8217;s not a lot I can do about that.</p>
<p>Here&#8217;s the one thing that I&#8217;m firm on: Ensuring that I have a set of tests that fully cover 100% of the unit that they <em>directly</em> test is a <strong>Good Thing</strong>.</p>
<p>And here&#8217;s the million dollar question: What can I do to prevent these kinds of mistakes in the future? Is it even possible?</p>
<p>To be honest, I&#8217;m OK making mistakes as long as I can catch and fix them quickly enough. Did I know about the <code>Part</code> and <code>LiteralText</code> classes before starting work on <code>Scanner</code>? I can&#8217;t actually remember. Let&#8217;s pretend I didn&#8217;t. As soon as I saw that the test for <code>Scanner</code> was referencing other classes, should I have stopped what I was doing, put an <code>Ignore</code> attribute on the test, and started working on tests for those other classes first? I&#8217;m not so sure about that. I feel like doing that might have negatively impacted the journey I had already embarked on.</p>
<p>So maybe &#8220;failing&#8221; at TDD in this way is expected? It&#8217;s rare when a class has no dependencies. If I&#8217;m using tools like NUnit, NCover, and NDepend, I should be able to catch my &#8220;mistakes&#8221; pretty quickly. This means that my unit tests have to be <em>fast</em> or I&#8217;ll rarely run them and, if that happens, my mistakes won&#8217;t be caught until much later. By that point, I&#8217;ll have too many mistakes to fix, I&#8217;ll be out of time, forced to move on to the next task, and, there I go, abandoning everything I believe in (about developing and testing)!</p>
<p>And my coworkers wonder why I hate our &#8220;unit tests&#8221; that read from and write to the database so much&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/10/29/i-fail-at-tdd/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Why won&#8217;t my iterator throw?</title>
		<link>http://jason.diamond.name/weblog/2010/10/27/why-wont-my-iterator-throw/</link>
		<comments>http://jason.diamond.name/weblog/2010/10/27/why-wont-my-iterator-throw/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 23:56:19 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=116</guid>
		<description><![CDATA[I ran into a spot of confusion last night doing some TDD on a method that returns an IEnumerable&#60;T>. I was using multiple yield return statements in it which made the method an iterator and not just a normal method. Even though I know how iterators work, I don&#8217;t use them enough to remember their [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a spot of confusion last night doing some TDD on a method that returns an <code>IEnumerable&lt;T></code>. I was using multiple <code>yield return</code> statements in it which made the method an <a href="http://msdn.microsoft.com/en-us/library/dscyy5s0.aspx">iterator</a> and not just a normal method.</p>
<p>Even though I <em>know</em> how iterators work, I don&#8217;t use them enough to remember their idiosyncrasies. The main one being that it&#8217;s easy to &#8220;invoke&#8221; them without actually executing any of the code inside them!</p>
<p>To illustrate this using a highly contrived example, imagine you wrote the following test:</p>
<pre class="brush:csharp">[Test]
public void It_throws_when_you_pass_in_null()
{
    Assert.Throws&lt;ArgumentNullException>(
        () => MyObject.MyMethod(null));
}</pre>
<p>And then implemented the method it tests it like so:</p>
<pre class="brush:csharp">public static IEnumerable&lt;object> MyMethod(object arg)
{
    if (arg == null)
        throw new ArgumentNullException("arg");

    yield return "whatever";
}</pre>
<p>Surprise! Your test fails.</p>
<p>Why? Because the code in your iterator doesn&#8217;t start executing until you invoke <code>GetEnumerator</code> on its return value then invoke <code>MoveNext</code> on that.</p>
<p>One really quick way to force these method calls to happen is to use the <code>ToArray</code> extension method:</p>
<pre class="brush:csharp">[Test]
public void It_throws_when_you_pass_in_null()
{
    Assert.Throws&lt;ArgumentNullException>(
        () => MyObject.MyMethod(null).ToArray());
}</pre>
<p><code>ToList</code> or manually iterating over the result with <code>foreach</code> would work just as well.</p>
<p>A better way to fix this is to change your implementation so that it uses two methods:</p>
<pre class="brush:csharp">public static IEnumerable&lt;object> MyMethod(object arg)
{
    if (arg == null)
        throw new ArgumentNullException("arg");

    return MyMethodHelper(arg);
}

private static IEnumerable&lt;object> MyMethodHelper(object arg)
{
    yield return "whatever";
}</pre>
<p>Written this way, <code>MyMethod</code> isn&#8217;t an iterator anymore. It&#8217;s just a plain old method that gets executed the way you&#8217;d expect it to. <code>MyMethodHelper</code> becomes the iterator. Its code won&#8217;t get executed until you start calling <code>MoveNext</code>, but that&#8217;s OK because the validation code you care about already ran.</p>
<p>Problem solved, right? Unfortunately, this wasn&#8217;t quite my <em>exact</em> problem.</p>
<p>My method was actually throwing <em>after</em> it did its argument checking and <em>while</em> it was yielding values. There&#8217;s really no solution (that I know of) to handle this without invoking <code>MoveNext</code> (or something else that will invoke it for you like <code>ToArray</code>) until whatever condition throws your exception is triggered.</p>
<p>Adding <code>ToArray</code> to my test wasn&#8217;t a big deal, but it took me a bit of time to figure out why it was failing. I was actually setting breakpoints in my method, running the test in the debugger, and tripping out when my breakpoints weren&#8217;t hitting.</p>
<p>Maybe going through the trouble of writing this post will save me 10 minutes next time I write an iterator.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/10/27/why-wont-my-iterator-throw/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New BehaveN Release</title>
		<link>http://jason.diamond.name/weblog/2010/09/03/new-behaven-release/</link>
		<comments>http://jason.diamond.name/weblog/2010/09/03/new-behaven-release/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 23:59:05 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[BehaveN]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=113</guid>
		<description><![CDATA[This is a minor, bug fix release. You can see the changes here. I&#8217;ve also updated the wiki with some more documentation.]]></description>
			<content:encoded><![CDATA[<p><a href="http://code.google.com/p/behaven/downloads/detail?name=BehaveN-2.0.1.102.zip">This</a> is a minor, bug fix release. You can see the changes <a href="http://code.google.com/p/behaven/source/browse/HISTORY.txt?spec=svnd804cddd71ef23bd7254c9f018ce3264cf12d343&#038;r=d804cddd71ef23bd7254c9f018ce3264cf12d343">here</a>.</p>
<p>I&#8217;ve also updated the <a href="http://code.google.com/p/behaven/w/list">wiki</a> with some more documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/09/03/new-behaven-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BehaveN</title>
		<link>http://jason.diamond.name/weblog/2010/07/11/behaven/</link>
		<comments>http://jason.diamond.name/weblog/2010/07/11/behaven/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 00:58:14 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[BehaveN]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=110</guid>
		<description><![CDATA[I&#8217;ve been using a Cucumber-inspired BDD framework for .NET called BehaveN at work for the past year. Today, I just released the next major version. There&#8217;s a little documentation on the wiki, including a tutorial, but there&#8217;s a lot left that I haven&#8217;t documented yet. I&#8217;ll be getting more and more documentation up as time [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using a <a href="http://cukes.info/">Cucumber</a>-inspired BDD framework for .NET called <a href="http://code.google.com/p/behaven/">BehaveN</a> at work for the past year. Today, I just released the next major version.</p>
<p>There&#8217;s a little documentation on the wiki, including a <a href="http://code.google.com/p/behaven/wiki/Tutorial">tutorial</a>, but there&#8217;s a lot left that I haven&#8217;t documented yet. I&#8217;ll be getting more and more documentation up as time goes on.</p>
<p>It&#8217;s been pretty positively received by our product owners, QA, and most of the developers who&#8217;ve been exposed to it. It&#8217;s being used by a few outside my company, but I haven&#8217;t made any effort on &#8220;advertising&#8221; it. If anybody&#8217;s interested in giving it a try, let me know and I&#8217;ll try to fill in any of the blanks you might have.</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/07/11/behaven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autotest for Python</title>
		<link>http://jason.diamond.name/weblog/2010/06/18/autotest-for-python/</link>
		<comments>http://jason.diamond.name/weblog/2010/06/18/autotest-for-python/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 19:11:51 +0000</pubDate>
		<dc:creator>Jason Diamond</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://jason.diamond.name/weblog/?p=104</guid>
		<description><![CDATA[I recently went looking for an autotest equivalent for Python. This question on StackOverflow pointed me to autonose. It wasn&#8217;t that easy to install using easy_install since one of its dependencies (snakefood) failed to install so I had to do that manually. Unfortunately, autonose has a few issues, especially when running on Windows. Since it [...]]]></description>
			<content:encoded><![CDATA[<p>I recently went looking for an autotest equivalent for Python.</p>
<p>This <a href="http://stackoverflow.com/questions/108892/is-there-something-like-autotest-for-python-unittests">question</a> on StackOverflow pointed me to <a href="http://github.com/gfxmonk/autonose/">autonose</a>. It wasn&#8217;t that easy to install using easy_install since one of its dependencies (snakefood) failed to install so I had to do that manually.</p>
<p>Unfortunately, autonose has a few issues, especially when running on Windows. Since it doesn&#8217;t appear to be updated anymore, I went searching for an alternative and found pyautotest, part of the <a href="http://www.metareal.org/p/modipyd/">Modipyd</a> project. It doesn&#8217;t use <a href="http://somethingaboutorange.com/mrl/projects/nose/">nose</a>, but that&#8217;s OK, because I wasn&#8217;t using anything that required it.</p>
<p>I had to install Modipyd by downloading its source from this <a href="http://github.com/ishikawa/modipyd">GitHub repository</a> and running <code>python setup.py install</code>.</p>
<p>Pyautotest is exactly what I was looking for&#8211;simple and works right out of the box without a nest of dependencies. The only part I was missing was support for <a href="http://www.growlforwindows.com/">Growl for Windows</a>.</p>
<p><a href="http://www.ianlewis.org/en/modipyd-growl-test-driven-development">Ian Lewis</a> and his co-workers released some <a href="http://bitbucket.org/ae35/growltestrunner/">custom test runners</a> that can be used via pyautotest. The Growl version uses the <a href="http://growl.info/documentation/growlnotify.php">growlnotify</a> tool, but that didn&#8217;t work with the version of <a href="http://www.growlforwindows.com/gfw/help/growlnotify.aspx">growlnotify.exe</a> that works on Windows.</p>
<p>I started modifying their runner to work with growlnotify.exe, but it really bothered me how they copied and pasted the entire contents of the unittest.TextTestRunner.run method into their derived class so I threw together my own version which doesn&#8217;t contain such a flagrant disregard for object-oriented principles. You can clone/fork it <a href="http://github.com/jdiamond/CustomTestRunners">here</a>.</p>
<p>For the icons, I used Jamie Hill&#8217;s pass/fail smilies which I found <a href="http://thelucid.com/2007/07/30/autotest-growl-fail-pass-smilies/">here</a>.</p>
<p>I hope this helps others trying to do TDD with Python. I absolutely love saving in Vim and seeing a green smiley face immediately appear. =)</p>
]]></content:encoded>
			<wfw:commentRss>http://jason.diamond.name/weblog/2010/06/18/autotest-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

