﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>More xUnit tweaks, dynamic test skipping</title><description>&lt;p&gt;For a long time, xUnit’s dev has &lt;a href="http://xunit.codeplex.com/workitem/9691"&gt;resisted&lt;/a&gt; adding support for skipping a test dynamically. You could create your own Fact class and handle that yourself, but that was quite a lot of work, for something very specific.&lt;/p&gt; &lt;p&gt;In RavenDB, we had the need to dynamically decide whatever the test can run based on the actual test situation, so I decided to add this to our xUnit fork. This turned out to be really simple to do. Just three lines of code &lt;img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Smile" src="http://ayende.com/blog/Images/Windows-Live-Writer/8bbe3d2fa751_C1D5/wlEmoticon-smile_2.png"&gt;&lt;/p&gt; &lt;p&gt;&lt;a title="https://github.com/ayende/xunit/commit/82accb4c850a3938187ac334fb73d6e81dc921e3#diff-3c2b9f2cb8392f32456d0bf81151b59fR57" href="https://github.com/ayende/xunit/commit/82accb4c850a3938187ac334fb73d6e81dc921e3#diff-3c2b9f2cb8392f32456d0bf81151b59fR57"&gt;https://github.com/ayende/xunit/commit/82accb4c850a3938187ac334fb73d6e81dc921e3#diff-3c2b9f2cb8392f32456d0bf81151b59fR57&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/166049/more-xunit-tweaks-dynamic-test-skipping?Key=1e0fb7c7-e66a-4ec1-a971-d4f991cdbae9</link><guid>http://ayende.com/166049/more-xunit-tweaks-dynamic-test-skipping?Key=1e0fb7c7-e66a-4ec1-a971-d4f991cdbae9</guid><pubDate>Wed, 19 Mar 2014 10:00:00 GMT</pubDate></item><item><title>Tweaking xUnit</title><description>&lt;p&gt;
	One of the interesting challenges that we have with RavenDB is the number and duration of our tests.&lt;/p&gt;
&lt;p&gt;
	In particular, we current have over three thousands tests, and they take &lt;em&gt;hours&lt;/em&gt; to run. We are doing a lot of stuff there &amp;ldquo;let us insert million docs, write a map/reduce index, query on that, then do a mass update, see what happens&amp;rdquo;, etc. We are also doing a lot of stuff that really can&amp;rsquo;t be emulated easily. If I&amp;rsquo;m testing replication for a non existent target, I need to check that actual behavior, etc. Oh, and we&amp;rsquo;re probably doing silly stuff in there, too.&lt;/p&gt;
&lt;p&gt;
	In order to try to increase our feedback cycle times, I made some modifications to xUnit. It is now going to record the test duration of the tests, the results look like that:&lt;/p&gt;
&lt;p&gt;
	&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Tweaking-xUnit_A686/image_2.png"&gt;&lt;img alt="image" border="0" height="260" src="http://ayende.com/blog/Images/Windows-Live-Writer/Tweaking-xUnit_A686/image_thumb.png" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" title="image" width="1240" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;
	You can see that Troy is taking too long. In fact, there is a bug that those tests currently expose that result in a timeout exception, that is why they take so long.&lt;/p&gt;
&lt;p&gt;
	But this is just to demonstrate the issue. The real power here is that we also use this when decided how to &lt;em&gt;run&lt;/em&gt; the tests. We are simply sorting them by how long they took to run. If we don&amp;rsquo;t have a record for that, we&amp;rsquo;ll give them a run time of &amp;ndash;1.&lt;/p&gt;
&lt;p&gt;
	This has a bunch of interesting implications:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;
		The faster tests are going to run first. That means that we&amp;rsquo;ll have earlier feedback if we broke something.&lt;/li&gt;
	&lt;li&gt;
		The new tests (haven&amp;rsquo;t had chance to run ever) will run first, those are were the problems are more likely anyway.&lt;/li&gt;
	&lt;li&gt;
		We only run report this for &lt;em&gt;passing&lt;/em&gt; tests, that means that we are going to run failed tests first as well.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
	In addition to that, this will also give us better feedback on what are slow tests are. So we can actually give them some attention and see if they are really required to be slow or they can be fixed.&lt;/p&gt;
&lt;p&gt;
	Hopefully, we can find a lot of the tests that are long, and just split them off into a separate test project, to be run at a later time.&lt;/p&gt;
&lt;p&gt;
	The important thing is, now we have the information to handle this.&lt;/p&gt;
</description><link>http://ayende.com/165922/tweaking-xunit?Key=d4f742d1-f971-4ba9-8726-160540455200</link><guid>http://ayende.com/165922/tweaking-xunit?Key=d4f742d1-f971-4ba9-8726-160540455200</guid><pubDate>Mon, 03 Mar 2014 14:00:00 GMT</pubDate></item><item><title>What does the test say?</title><description>&lt;p&gt;Because RavenDB is a database, a lot of the tests we have to run are pretty long. For example, we need to touch the disk a lot, and we have a &lt;em&gt;lot&lt;/em&gt; of networked tests.&lt;/p&gt; &lt;p&gt;that means that running this test suite can take a while. But the default information we get is pretty lousy. Just the test count and that is it. But when a test hang, and they do if we have bugs, it make it very hard to figure out where the culprit is.&lt;/p&gt; &lt;p&gt;So we forked xunit and added a tiny feature to the console runner:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/46088279ac45_75B3/image_4.png"&gt;&lt;img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/46088279ac45_75B3/image_thumb_1.png" width="697" height="155"&gt;&lt;/a&gt;&lt;/p&gt;</description><link>http://ayende.com/165762/what-does-the-test-say?Key=f889e6e4-0047-4238-a957-3c4bb3859ab6</link><guid>http://ayende.com/165762/what-does-the-test-say?Key=f889e6e4-0047-4238-a957-3c4bb3859ab6</guid><pubDate>Mon, 03 Mar 2014 10:00:00 GMT</pubDate></item><item><title>Defensive coding is your friend</title><description>&lt;p&gt;We just had a failing test:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Defensive-coding-is-your-friend_B61B/image_2.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 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://ayende.com/blog/Images/Windows-Live-Writer/Defensive-coding-is-your-friend_B61B/image_thumb.png" width="843" height="65"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;As you can see we assumed that fiddler is running, when it isn’t. Here is the bug:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Defensive-coding-is-your-friend_B61B/image_4.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 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://ayende.com/blog/Images/Windows-Live-Writer/Defensive-coding-is-your-friend_B61B/image_thumb_1.png" width="733" height="244"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Now, this is great when I am testing things out, and want to check what is going on the wire using Fiddler, but I always have to remember to revert this change, otherwise we will have a failing test and a failing build.&lt;/p&gt; &lt;p&gt;That isn’t very friction free, so I added the following:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/Defensive-coding-is-your-friend_B61B/image_6.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 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://ayende.com/blog/Images/Windows-Live-Writer/Defensive-coding-is-your-friend_B61B/image_thumb_2.png" width="578" height="212"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Now the code is smart enough to not fail the test if we didn’t do things right.&lt;/p&gt;</description><link>http://ayende.com/160898/defensive-coding-is-your-friend?Key=606f3fbb-4e29-4517-9cf6-3ed9befa2dc9</link><guid>http://ayende.com/160898/defensive-coding-is-your-friend?Key=606f3fbb-4e29-4517-9cf6-3ed9befa2dc9</guid><pubDate>Tue, 12 Feb 2013 10:00:00 GMT</pubDate></item><item><title>On failing tests</title><description>&lt;p&gt;I made a change (deep in the guts of RavenDB), and then I run the tests, and I go this:&lt;/p&gt; &lt;p&gt;&lt;a href="http://ayende.com/blog/Images/Windows-Live-Writer/On-failing-tests_85B0/image_2.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://ayende.com/blog/Images/Windows-Live-Writer/On-failing-tests_85B0/image_thumb.png" width="473" height="85"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;I &lt;em&gt;love* &lt;/em&gt;it when this happens, because it means that there is one root cause that I need to fix, really obvious and in the main code path.&lt;/p&gt; &lt;p&gt;I hate it when there is just one failing test, because it means that this is an edge condition or something freaky like that.&lt;/p&gt; &lt;p&gt;* obviously I would love it more if there were no failing tests.&lt;/p&gt;</description><link>http://ayende.com/160929/on-failing-tests?Key=7ff72b47-5982-460b-829c-d1374f06d280</link><guid>http://ayende.com/160929/on-failing-tests?Key=7ff72b47-5982-460b-829c-d1374f06d280</guid><pubDate>Thu, 07 Feb 2013 10:00:00 GMT</pubDate></item><item><title>Testing Rhino Service Bus applications</title><description>&lt;p&gt;One of the really nice things about Rhino Service Bus applications is that we have created a structured way to handle inputs and outputs. You have messages coming in and out, as well as the endpoint local state to deal with. You don’t have to worry about how to deal with external integration points, because those are already going over messages.&lt;/p&gt; &lt;p&gt;And when you have basic input/output figured out, you are pretty much done.&lt;/p&gt; &lt;p&gt;For example, let us see the code that handles extending trail licenses in our ordering system:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ExtendTrialLicenseConsumer : ConsumerOf&amp;lt;ExtendTrialLicense&amp;gt;
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; IDocumentSession Session { get; set; }
    &lt;span class="kwrd"&gt;public&lt;/span&gt; IServiceBus Bus { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Consume(ExtendTrialLicense message)
    {
        var productId = message.ProductId ?? &lt;span class="str"&gt;"products/"&lt;/span&gt; + message.Profile;
        var trial = Session.Query&amp;lt;Trial&amp;gt;()
            .Where(x =&amp;gt; x.Email == message.Email &amp;amp;&amp;amp; x.ProductId == productId)
            .FirstOrDefault();

        &lt;span class="kwrd"&gt;if&lt;/span&gt; (trial == &lt;span class="kwrd"&gt;null&lt;/span&gt;)
            &lt;span class="kwrd"&gt;return&lt;/span&gt;;
        
        trial.EndsAt = DateTime.Today.AddDays(message.Days);
        Bus.Send(&lt;span class="kwrd"&gt;new&lt;/span&gt; NewTrial
        {
            ProductId = productId,
            Email = trial.Email,
            Company = trial.Company,
            FullName = trial.Name,
            TrackingId = trial.TrackingId
        });
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;How do we test something like this? As it turns out, quite easily:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TrailTesting : ConsumersTests
{
    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PrepareData(IDocumentSession session)
    {
        session.Store(&lt;span class="kwrd"&gt;new&lt;/span&gt; Trial
        {
            Email = &lt;span class="str"&gt;"you@there.gov"&lt;/span&gt;,
            EndsAt = DateTime.Today,
            ProductId = &lt;span class="str"&gt;"products/nhprof"&lt;/span&gt;
        });
    }

    [Fact]
    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Will_update_trial_date()
    {
        Consume&amp;lt;ExtendTrialLicenseConsumer, ExtendTrialLicense&amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; ExtendTrialLicense
        {
            ProductId = &lt;span class="str"&gt;"products/nhprof"&lt;/span&gt;,
            Days = 30,
            Email = &lt;span class="str"&gt;"you@there.gov"&lt;/span&gt;,
        });

        &lt;span class="kwrd"&gt;using&lt;/span&gt; (var session = documentStore.OpenSession())
        {
            var trial = session.Load&amp;lt;Trial&amp;gt;(1);
            Assert.Equal(DateTime.Today.AddDays(30), trial.EndsAt);
        }
    }

    &lt;span class="rem"&gt;// more tests here&lt;/span&gt;
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;All the magic happens in the base class, though:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;abstract&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; ConsumersTests : IDisposable
{
    &lt;span class="kwrd"&gt;protected&lt;/span&gt; IDocumentStore documentStore;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; IServiceBus Bus = &lt;span class="kwrd"&gt;new&lt;/span&gt; FakeBus();

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; ConsumersTests()
    {
        documentStore = &lt;span class="kwrd"&gt;new&lt;/span&gt; EmbeddableDocumentStore
        {
            RunInMemory = &lt;span class="kwrd"&gt;true&lt;/span&gt;,
            Conventions =
                {
                    DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites
                }
        }.Initialize();

        IndexCreation.CreateIndexes(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(Products_Stats).Assembly, documentStore);

        Products.Create(documentStore);
        &lt;span class="kwrd"&gt;using&lt;/span&gt; (var session = documentStore.OpenSession())
        {
            PrepareData(session);
            session.SaveChanges();
        }
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; T ConsumeSentMessage&amp;lt;T&amp;gt;()
    {
        var fakeBus = ((FakeBus)Bus);
        &lt;span class="kwrd"&gt;object&lt;/span&gt; o = fakeBus.Messages.Where(x =&amp;gt; x.GetType() == &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T)).First();

        fakeBus.Messages.Remove(o);
        &lt;span class="kwrd"&gt;return&lt;/span&gt; (T) o;
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Consume&amp;lt;TConsumer, TMsg&amp;gt;(TMsg msg)
        &lt;span class="kwrd"&gt;where&lt;/span&gt; TConsumer : ConsumerOf&amp;lt;TMsg&amp;gt;, &lt;span class="kwrd"&gt;new&lt;/span&gt;()
    {
        var foo = &lt;span class="kwrd"&gt;new&lt;/span&gt; TConsumer();

        &lt;span class="kwrd"&gt;using&lt;/span&gt; (var documentSession = documentStore.OpenSession())
        {
            Set(foo, documentSession);
            Set(foo, Bus);
            Set(foo, documentStore);

            foo.Consume(msg);

            documentSession.SaveChanges();
        }
    }

    &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Set&amp;lt;T,TValue&amp;gt;(T foo, TValue &lt;span class="kwrd"&gt;value&lt;/span&gt;)
    {
        PropertyInfo firstOrDefault = &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(T).GetProperties().FirstOrDefault(x=&amp;gt;x.PropertyType==&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(TValue));
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (firstOrDefault == &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;return&lt;/span&gt;;
        firstOrDefault.SetValue(foo, &lt;span class="kwrd"&gt;value&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;);
    }

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; PrepareData(IDocumentSession session)
    {
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Dispose()
    {
        documentStore.Dispose();
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And here are the relevant details for the FakeBus implementation:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; FakeBus : IServiceBus
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;  Messages = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;&lt;span class="kwrd"&gt;object&lt;/span&gt;&amp;gt;();

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Send(&lt;span class="kwrd"&gt;params&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] messages)
    {
        Messages.AddRange(messages);
    }
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;Now, admittedly, this is a fairly raw approach and we can probably do better. This is basically hand crafted auto mocking for consumers, and I don’t like the Consume&amp;lt;TConsumer,TMsg&amp;gt;() syntax very much. But it works, it is simple and it doesn’t really gets in the way.&lt;/p&gt;
&lt;p&gt;I won’t say it is &lt;em&gt;the&lt;/em&gt; way to go about it, but it is certainly easier than many other efforts that I have seen. We just need to handle the inputs &amp;amp; outputs and have a way to look at the local state, and you are pretty much done.&lt;/p&gt;</description><link>http://ayende.com/152993/testing-rhino-service-bus-applications?Key=ee11d848-dadc-4923-b8de-78e6a7813888</link><guid>http://ayende.com/152993/testing-rhino-service-bus-applications?Key=ee11d848-dadc-4923-b8de-78e6a7813888</guid><pubDate>Mon, 07 May 2012 09:00:00 GMT</pubDate></item><item><title>Structuring your Unit Tests, why?</title><description>&lt;p&gt;I am a strong believer in automated &lt;strike&gt;unit &lt;/strike&gt;tests. And I read &lt;a href="http://haacked.com/archive/2012/01/02/structuring-unit-tests.aspx"&gt;this post by Phil Haack&lt;/a&gt; with part amusement and part wonder. &lt;/p&gt; &lt;p&gt;RavenDB currently has close to 1,400 tests in it. We routinely ask for failing tests from users and fix bugs by writing tests to verify fixes.&lt;/p&gt; &lt;p&gt;But structuring them in terms of source code? That seems to be very strange.&lt;/p&gt; &lt;p&gt;You can take a look at the source code layout of some of our tests here: &lt;a href="https://github.com/ayende/ravendb/tree/master/Raven.Tests/Bugs"&gt;https://github.com/ayende/ravendb/tree/master/Raven.Tests/Bugs&lt;/a&gt;&lt;/p&gt; &lt;p&gt;It is a dumping ground, basically, for tests. That is, for the most part, I view tests as very important in telling me “does this !@#! works or not?” and that is about it. Spending a lot of time organizing them seems to be something of little value from my perspective.&lt;/p&gt; &lt;p&gt;If I need to find a particular test, I have R# code browsing to help me, and if I need to find who is testing a piece of code, I can use Find References to get it.&lt;/p&gt; &lt;p&gt;At the end, it boils down to the fact that I don’t consider tests to be, by themselves, a value to the product. Their only value is their binary ability to tell me whatever the product is okay or not. Spending a lot of extra time on the tests distract from creating &lt;em&gt;real &lt;/em&gt;value, shippable software.&lt;/p&gt; &lt;p&gt;What I &lt;em&gt;do&lt;/em&gt; deeply care about with regards to structuring the tests is the actual structure of the &lt;em&gt;test&lt;/em&gt;. It is important to make sure that all the tests looks very much the same, because I should be able to look at any of them and figure out what is going on rapidly.&lt;/p&gt; &lt;p&gt;I am not going to use the RavenDB example, because that is system software and usually different from most business apps (although we use a similar approach there). Instead, here are a few tests from our new ordering system:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[Fact]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Will_send_email_after_trial_extension()
{
    Consume&amp;lt;ExtendTrialLicenseConsumer, ExtendTrialLicense&amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; ExtendTrialLicense
    {
        ProductId = &lt;span class="str"&gt;"products/nhprof"&lt;/span&gt;,
        Days = 30,
        Email = &lt;span class="str"&gt;"you@there.gov"&lt;/span&gt;,
    });

    var email = ConsumeSentMessage&amp;lt;NewTrial&amp;gt;();

    Assert.Equal(&lt;span class="str"&gt;"you@there.gov"&lt;/span&gt;, email.Email);
}

[Fact]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Trial_request_from_same_customer_sends_email()
{
    Consume&amp;lt;NewTrialConsumer, NewTrial&amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; NewTrial
    {
        ProductId = &lt;span class="str"&gt;"products/nhprof"&lt;/span&gt;,
        Email = &lt;span class="str"&gt;"who@is.there"&lt;/span&gt;,
        Company = &lt;span class="str"&gt;"h"&lt;/span&gt;,
        FullName = &lt;span class="str"&gt;"a"&lt;/span&gt;,
        TrackingId = Guid.NewGuid()
    });
    Trial firstTrial;
    &lt;span class="kwrd"&gt;using&lt;/span&gt; (var session = documentStore.OpenSession())
    {
        firstTrial = session.Load&amp;lt;Trial&amp;gt;(1);
    }
    Assert.NotNull(ConsumeSentMessage&amp;lt;SendEmail&amp;gt;());
    
    Consume&amp;lt;NewTrialConsumer, NewTrial&amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; NewTrial
    {
        TrackingId = firstTrial.TrackingId,
        Email = firstTrial.Email,
        Profile = firstTrial.ProductId.Substring(&lt;span class="str"&gt;"products/"&lt;/span&gt;.Length)
    });

    var email = ConsumeSentMessage&amp;lt;SendEmail&amp;gt;();
    Assert.Equal(&lt;span class="str"&gt;"Hibernating Rhinos - Trials Agent"&lt;/span&gt;, email.ReplyToDisplay);
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;As you can probably see, we have a structured way to send input to the system, and we can verify the output and the side affects (creating the trial, for example).&lt;/p&gt;
&lt;p&gt;This leads to a system that can be easily tested, but doesn’t force us to spend too much time in the ceremony of tests.&lt;/p&gt;</description><link>http://ayende.com/152897/structuring-your-unit-tests-why?Key=580f908e-0bb1-44d6-86ee-b9167ee03147</link><guid>http://ayende.com/152897/structuring-your-unit-tests-why?Key=580f908e-0bb1-44d6-86ee-b9167ee03147</guid><pubDate>Mon, 02 Jan 2012 06:48:00 GMT</pubDate></item><item><title>Async tests in Silverlight</title><description>&lt;p&gt;One of the things that we do is build a lot of stuff in Silverlight, usually, those things are either libraries or UI. Testing Silverlight was always a problem, but at least there is&lt;em&gt; a&lt;/em&gt; solution OOTB for that.&lt;/p&gt; &lt;p&gt;Unfortunately, the moment that you start talking about async tests (for example, you want to run a web server to check things), you need to do things like this, EnqueueCallback, EnqueueConditional and other stuff that makes the test nearly impossible to read.&lt;/p&gt; &lt;p&gt;Luckily for us, Christopher Bennage stopped here for a while and &lt;a href="http://devlicio.us/blogs/christopher_bennage/archive/2011/01/17/improving-asynchronous-tests-for-silverlight.aspx"&gt;created a solution&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;It allows you to take the following sync test:&lt;/p&gt; &lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[Fact]
&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CanUpload()
{
    var ms = &lt;span class="kwrd"&gt;new&lt;/span&gt; MemoryStream();
    var streamWriter = &lt;span class="kwrd"&gt;new&lt;/span&gt; StreamWriter(ms);
    var expected = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;(&lt;span class="str"&gt;'a'&lt;/span&gt;,1024);
    streamWriter.Write(expected);
    streamWriter.Flush();
    ms.Position = 0;

    var client = NewClient(); 
    client.UploadAsync(&lt;span class="str"&gt;"abc.txt"&lt;/span&gt;, ms).Wait();

    var downloadString = webClient.DownloadString(&lt;span class="str"&gt;"/files/abc.txt"&lt;/span&gt;);
    Assert.Equal(expected, downloadString);
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And translate it to:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;[Asynchronous]
&lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;Task&amp;gt; CanUpload()
{
    var ms = &lt;span class="kwrd"&gt;new&lt;/span&gt; MemoryStream();
    var streamWriter = &lt;span class="kwrd"&gt;new&lt;/span&gt; StreamWriter(ms);
    var expected = &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;(&lt;span class="str"&gt;'a'&lt;/span&gt;, 1024);
    streamWriter.Write(expected);
    streamWriter.Flush();
    ms.Position = 0;

    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; client.UploadAsync(&lt;span class="str"&gt;"abc.txt"&lt;/span&gt;, ms);

    var async = webClient.DownloadStringTaskAsync(&lt;span class="str"&gt;"/files/abc.txt"&lt;/span&gt;);
    &lt;span class="kwrd"&gt;yield&lt;/span&gt; &lt;span class="kwrd"&gt;return&lt;/span&gt; async;

    Assert.AreEqual(expected, async.Result);
}
&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;It makes things &lt;em&gt;so much easier&lt;/em&gt;. To set this us, just reference &lt;a href="https://github.com/bennage/Raven-Custom-Silverlight-Unit-Test-Provider"&gt;the project&lt;/a&gt; and add the following in the App.xaml.cs file:&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Application_Startup(&lt;span class="kwrd"&gt;object&lt;/span&gt; sender, StartupEventArgs e)
{
&lt;font style="background-color: #ffff00"&gt;    UnitTestSystem.RegisterUnitTestProvider(&lt;span class="kwrd"&gt;new&lt;/span&gt; RavenCustomProvider());&lt;/font&gt;
    RootVisual = UnitTestSystem.CreateTestPage();
}&lt;/pre&gt;
&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;
&lt;/blockquote&gt;
&lt;p&gt;And you get tests that are now easy to write and run in Silverlight.&lt;/p&gt;</description><link>http://ayende.com/141313/async-tests-in-silverlight?Key=3d7755e1-075d-4428-a99b-079146042683</link><guid>http://ayende.com/141313/async-tests-in-silverlight?Key=3d7755e1-075d-4428-a99b-079146042683</guid><pubDate>Fri, 02 Dec 2011 10:00:00 GMT</pubDate></item><item><title>Cut the abstractions by putting test hooks</title><description>&lt;p&gt;I have been hearing about testable code for a long time now. It looks somewhat like this, although I had to cut on the number of interfaces along the way.&lt;/p&gt;  &lt;p&gt;&lt;img src="http://yuml.me/diagram/scruffy%3Bdir%3ALR%3B/class/%5BIUserRepository%7CGet%28id%29%3BFindByEmailFetchGroups%28email%29%3B%5D%2C%20%5BNHiberanteUserRepository%7CGet%28id%29%3BFindByEmailFetchGroups%28email%29%3B%5D%2C%20%5BInMemoryUserRepository%7CGet%28id%29%3BFindByEmailFetchGroups%28email%29%3B%5D%2C%20%5BLoginController%7CLogin%28%29%5D%2C%20%5BIAuthenticationService%7CIsValidLogin%28user%20pass%29%5D%2C%20%5BLdapAuthenticationService%7CIsValidLogin%28user%20pass%29%5D%2C%20%5BLoginController%5D%5E%5BIAuthenticationService%5D%2C%20%5BLoginController%5D%5E%5BIUserRepository%5D%2C%20%5BLdapAuthenticationService%5D%5E%5BIUserRepository%5D%2C%20%5BIUserRepository%5D%5E%5BNHiberanteUserRepository%5D%2C%20%5BIUserRepository%5D%5E%5BInMemoryUserRepository%5D%2C%20%5BIAuthenticationService%5D%5E%5BLdapAuthenticationService%5D.png" width="1024" height="102" /&gt;&lt;/p&gt;  &lt;p&gt;We go through a lot of contortions to be able to do something fairly simple, avoid hitting a data store in our tests.&lt;/p&gt;  &lt;p&gt;This is actually inaccurate, we are putting in a lot of effort into being able to do that &lt;em&gt;without changing production code&lt;/em&gt;. There are even a lot of explanations how testable code is decoupled, and easy to change, etc.&lt;/p&gt;  &lt;p&gt;In my experience, one common problem is that we put in &lt;em&gt;too&lt;/em&gt; much abstraction in our code. Sometimes it actually serve a purpose, but in many cases, it is just something that we do to enable testing. But we still pay the hit in the design complexity anyway.&lt;/p&gt;  &lt;p&gt;We can throw all of that away, and keep only what we need to run production code, but that would mean that we would have harder time with the tests. But we can resolve the issue very easily by making my &lt;em&gt;infrastructure&lt;/em&gt; aware of testing, such as this example:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Cuttheabstractionsbyputtingtesthooks_854C/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Cuttheabstractionsbyputtingtesthooks_854C/image_thumb_1.png" width="871" height="269" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;But now your production code was changed by tests?!&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Yes, it was, so? I never really got the problem people had with this, but at this day and age, putting in the hooks to make testing easier just make sense. Yes, you can go with the “let us add abstractions until we can do it”, but it is much cheaper and faster to go with this approach.&lt;/p&gt;  &lt;p&gt;Moreover, notice that this is part of the infrastructure code, which I don’t consider as production code (you don’t touch it very often, although of course it has to be production ready), so I don’t have any issue with this.&lt;/p&gt;  &lt;p /&gt;  &lt;blockquote&gt;   &lt;p&gt;Nitpicker corner: Let us skip the whole TypeMock discussion, shall we?&lt;/p&gt;    &lt;p&gt;TDD fanatic corner: I don’t really care about testable code, I care about &lt;em&gt;tested&lt;/em&gt; code. If I have a regression suite, that is just what i need.&lt;/p&gt;&lt;/blockquote&gt;</description><link>http://ayende.com/4429/cut-the-abstractions-by-putting-test-hooks?Key=8d225391-8c56-4acd-b647-341fd77bc1dd</link><guid>http://ayende.com/4429/cut-the-abstractions-by-putting-test-hooks?Key=8d225391-8c56-4acd-b647-341fd77bc1dd</guid><pubDate>Tue, 09 Mar 2010 10:00:00 GMT</pubDate></item><item><title>Hey you, developer! Learn a bit of business speak!</title><description>&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/HeyyoudeveloperLearnabitofbusinessspeak_12E9/image_4.png"&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="image" border="0" alt="image" align="right" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/HeyyoudeveloperLearnabitofbusinessspeak_12E9/image_thumb_1.png" width="184" height="195" /&gt;&lt;/a&gt;  In a &lt;a href="http://ayende.com/Blog/archive/2009/09/18/11106.aspx#34653"&gt;comment&lt;/a&gt; to one of my posts, Dave Mertens said:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;I often wish I had the luxury the throw away the current code and start from scratch. Why. Because my manager ask me how long it takes to fix just this bug. 80% of the bugs (luckily we don't have that many bugs) are fixed within 2 hours including writing tests. Than he asks me how long it takes to rewrite the damn code.&lt;/p&gt; &lt;/blockquote&gt;  &lt;blockquote /&gt;  &lt;p&gt;It is interesting to note that the questions you ask select the answers you want.&lt;/p&gt;  &lt;p&gt;When you give someone a choice between 2 hours of fixing code vs. 3 days of fixing code, the decision is &lt;em&gt;always&lt;/em&gt; going to be in favor of 2 hours.&lt;/p&gt;  &lt;p&gt;At one point in my life I was asked if I stopped beating up people. This is a twist on the old “have you stopped beating your wife yet? yes/no” question. I answered in Mu. Then I spent another ten minutes explaining &lt;a href="http://en.wikipedia.org/wiki/Mu_%28negative%29"&gt;what I meant&lt;/a&gt;. &lt;/p&gt;  &lt;p /&gt;  &lt;p&gt;When someone asks me those types of questions, I correct the question, before I answer. Because while you may ask questions to lead the answer in a certain way, there is nothing that says that I must go that way.&lt;/p&gt;  &lt;p&gt;The &lt;em&gt;right &lt;/em&gt;question is: How often are we hitting on bugs for this same code path? &lt;em&gt;Then&lt;/em&gt; asking about how long it takes to fix the whole damn thing.&lt;/p&gt;  &lt;p&gt;Oh, and make sure that you &lt;em&gt;can&lt;/em&gt; fix it. There is nothing a business likes less than spending time &amp;amp; money on solving a problem and ending up not solving it. Leaving aside what it does to your credibility and the trust relationship with the business.&lt;/p&gt;</description><link>http://ayende.com/4223/hey-you-developer-learn-a-bit-of-business-speak?Key=f1989a7e-6766-4531-beb2-65000a2d98ed</link><guid>http://ayende.com/4223/hey-you-developer-learn-a-bit-of-business-speak?Key=f1989a7e-6766-4531-beb2-65000a2d98ed</guid><pubDate>Thu, 01 Oct 2009 22:10:00 GMT</pubDate></item><item><title>re: Are you smart enough to do without TDD</title><description>&lt;p&gt;Daniel has posted a reply to my &lt;a href="http://ayende.com/Blog/archive/2009/09/28/even-tests-has-got-to-justify-themselves.aspx"&gt;post&lt;/a&gt;, titling it:  &lt;a href="http://www.clariusconsulting.net/blogs/kzu/archive/2009/09/29/AreyousmartenoughtodowithoutTDD.aspx"&gt;Are you smart enough to do without TDD&lt;/a&gt;. I more or less expected to get responses like that, which was why I was hesitant to  post it. Contrary to popular opinion, I don’t really enjoy being controversial. &lt;/p&gt;  &lt;p&gt;There are two main points that I object to in his post:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;You see, Ayende appears to say that if you're smart enough, you'll just know what code to write, just like that. Ergo, if you don't know, maybe you're not that smart and hence you would need this technique for losers called Test Driven Design/Development.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;That is not what I said, please don’t put words in my mouth. What I &lt;em&gt;said&lt;/em&gt; was: “The idea behind TDD is to use the tests to drive the design. Well, in this case, I don’t have any design to drive.” Combine this with my &lt;a href="http://ayende.com/Blog/archive/2009/03/06/application-structure-concepts-amp-features.aspx"&gt;concepts &amp;amp; features&lt;/a&gt; architecture, where the main tenets is: “&lt;strong&gt;A feature creation may not involve any design activity.&lt;/strong&gt;” and it should be clear why TDD simply doesn’t work for my scenario. &lt;/p&gt;  &lt;p&gt;And his attack on Rhino Mocks:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;a href="http://moq.me"&gt;Moq&lt;/a&gt; vs &lt;a href="http://www.mockframeworks.com/rhino"&gt;Rhino Mocks&lt;/a&gt;: he [Ayende, it seems] read the (&lt;a href="http://www.clariusconsulting.net/blogs/kzu/archive/2007/12/27/48594.aspx"&gt;useless IMO&lt;/a&gt;) literature on mocks vs stubs vs fakes, had apparently a clear idea of what to do, and came up with Rhino's awkward, user unfriendly and hard to learn API with a myriad of concepts and options, and a record-replay-driven API (ok, I'm sure it was not his original idea, but certainly it's his impl.) which two years ago seemed to him to &lt;a href="http://ayende.com/Blog/archive/2007/12/26/The-RecordReplayVerify-model.aspx"&gt;stand at the core of mocking&lt;/a&gt;. Nowadays not only he learned what I've been saying all along, that "&lt;a href="http://ayende.com/Blog/archive/2009/09/03/planning-for-rhino-mocks-4.0.aspx"&gt;dynamic, strict, partial and stub... No one cares&lt;/a&gt;", but also is planning to remove the &lt;a href="http://ayende.com/Blog/archive/2009/09/03/planning-for-rhino-mocks-4.0.aspx"&gt;record / playback API&lt;/a&gt; too.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This is just &lt;em&gt;full&lt;/em&gt; of misinformation. Let me see how:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Rhino Mocks is &lt;em&gt;5 years old&lt;/em&gt;. &lt;/li&gt;    &lt;li&gt;Rhino Mocks came out for .NET 1.0.&lt;/li&gt;    &lt;li&gt;Rhino Mocks actually predate most of the mocks vs. stubs debate.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I keep Rhino Mocks updated as new concepts and syntax options comes. Yes, AAA is easier, but AAA relies on having the syntax options that we have in C# 3.0. Rhino Mocks didn’t start from there, it started a lot earlier, and it is a testament to its flexibility that I was able to adapt it to any change along the way.&lt;/p&gt;  &lt;p&gt;Oh, and &lt;em&gt;Rhino Mocks&lt;/em&gt; was developed with TDD, fully. Still is, for that matter. So I find it annoying that someone attacks it on this grounds without really understanding how it worked.&lt;/p&gt;</description><link>http://ayende.com/4220/re-are-you-smart-enough-to-do-without-tdd?Key=860d1733-6c51-4b59-aab6-b23f8501f857</link><guid>http://ayende.com/4220/re-are-you-smart-enough-to-do-without-tdd?Key=860d1733-6c51-4b59-aab6-b23f8501f857</guid><pubDate>Wed, 30 Sep 2009 06:33:20 GMT</pubDate></item><item><title>Scenario Driven Tests</title><description>&lt;p&gt;I originally titled this blog post: Separate the scenario under test from the asserts. I intentionally use the terminology scenario under test, instead of calling it class or method under test. &lt;/p&gt;  &lt;p&gt;One of the main problem with unit testing is that we are torn between competing forces. One is the usual drive for abstraction and eradication of duplication, the second is clarity of the test itself. &lt;a href="http://codebetter.com/blogs/karlseguin/"&gt;Karl Seguin&lt;/a&gt; does a good job covering that &lt;a href="http://codebetter.com/blogs/karlseguin/archive/2009/09/12/unit-testing-do-repeat-yourself.aspx"&gt;conflict&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;I am dealing with the issue by the simple expedient of forbidding anything but asserts in the test method. And no, I don’t mean something like BDD, where the code under test is being setup in the constructor or the context initialization method.&lt;/p&gt;  &lt;p&gt;I tend to divide my tests code into four distinct parts:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Scenario under test&lt;/li&gt;    &lt;li&gt;Scenario executer&lt;/li&gt;    &lt;li&gt;Test model, represent the state of the application&lt;/li&gt;    &lt;li&gt;Test code itself, asserting the result of a specific scenario on the test model&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The problem is that a single scenario in the application may very well have multiple things that we want to actually test. Let us take the example of authenticating a user, there are several things that happen during the process of authentication, such as the actual authentication, updating the last login date, resetting bad login attempts, updating usage statistics, etc.&lt;/p&gt;  &lt;p&gt;I am going to write the code to test all of those scenarios first, and then discuss the roles of each item in the list. I think it will be clearer to discuss it when you have the code in front of you.&lt;/p&gt;  &lt;p&gt;We will start with the scenarios:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div id="codeSnippetWrapper"&gt;     &lt;pre id="codeSnippet" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; LoginSuccessfully : IScenario&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute(ScenarioContext context)&lt;br /&gt;    {&lt;br /&gt;        context.Login(&lt;span class="str"&gt;"my-user"&lt;/span&gt;,&lt;span class="str"&gt;"swordfish is a bad password"&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TryLoginWithBadPasswordTwice : IScenario&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute(ScenarioContext context)&lt;br /&gt;    {&lt;br /&gt;        context.Login(&lt;span class="str"&gt;"my-user"&lt;/span&gt;,&lt;span class="str"&gt;"bad pass"&lt;/span&gt;);&lt;br /&gt;        context.Login(&lt;span class="str"&gt;"my-user"&lt;/span&gt;,&lt;span class="str"&gt;"bad pass"&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; TryLoginWithBadPasswordTwiceThenTryWithRealPassword : IScenario&lt;br /&gt;{&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Execute(ScenarioContext context)&lt;br /&gt;    {&lt;br /&gt;        context.Login(&lt;span class="str"&gt;"my-user"&lt;/span&gt;,&lt;span class="str"&gt;"bad pass"&lt;/span&gt;);&lt;br /&gt;        context.Login(&lt;span class="str"&gt;"my-user"&lt;/span&gt;,&lt;span class="str"&gt;"bad pass"&lt;/span&gt;);&lt;br /&gt;        context.Login(&lt;span class="str"&gt;"my-user"&lt;/span&gt;,&lt;span class="str"&gt;"swordfish is a bad password"&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;And a few tests that would show the common usage:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;div id="codeSnippetWrapper"&gt;
    &lt;pre id="codeSnippet" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AuthenticationTests : ScenarioTests&lt;br /&gt;{&lt;br /&gt;    [Fact]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WillUpdateLoginDateOnSuccessfulLogin()&lt;br /&gt;    {&lt;br /&gt;        ExecuteScenario&amp;lt;LoginSuccessfully&amp;gt;();&lt;br /&gt;        &lt;br /&gt;        Assert.Equal(CurrentTime, model.CurrentUser.LastLogin);&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;&lt;br /&gt;    [Fact]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WillNotUpdateLoginDateOnFailedLogin()&lt;br /&gt;    {&lt;br /&gt;        ExecuteScenario&amp;lt;TryLoginWithBadPasswordTwice&amp;gt;();&lt;br /&gt;        &lt;br /&gt;        Assert.NotEqual(CurrentTime, model.CurrentUser.LastLogin);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Fact]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WillUpdateBadLoginCountOnFailedLogin()&lt;br /&gt;    {&lt;br /&gt;        ExecuteScenario&amp;lt;TryLoginWithBadPasswordTwice&amp;gt;();&lt;br /&gt;        &lt;br /&gt;        Assert.NotEqual(2, model.CurrentUser.BadLoginCount);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [Fact]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; CanSuccessfullyLoginAfterTwoFailedAttempts()&lt;br /&gt;    {&lt;br /&gt;        ExecuteScenario&amp;lt;TryLoginWithBadPasswordTwiceThenTryWithRealPassword&amp;gt;();&lt;br /&gt;        &lt;br /&gt;        Assert.True(model.CurrentUser.IsAuthenticated);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;As you can see, each of the tests is pretty short and to the point, there is a clear distinction between what we are testing and what is being tested.&lt;/p&gt;

&lt;p&gt;Each scenario represent some action in the system which we want to verify behavior for. Those are usually written with the help of a scenario context (or something of the like) with gives the scenario access to the application services required to perform its work. An alternative to the scenario context is to use a container in the tests and supply the application service implementations from there.&lt;/p&gt;

&lt;p&gt;The executer (ExecuteScenario&amp;lt;TScenario&amp;gt;() method) is responsible for setting the environment for the scenario, executing the scenario, and cleaning up afterward. It is also responsible for any updates necessary to get the test model up to date.&lt;/p&gt;

&lt;p&gt;The test model represent the state of the application after the scenario was executed. It is meant for the tests to be able to assert against. In many cases, you can use the actual model from the application, but there are cases where you would want to augment that with test specific items, to allow easier testing.&lt;/p&gt;

&lt;p&gt;And the tests, well, the tests simple execute a scenario and assert on the result.&lt;/p&gt;

&lt;p&gt;By abstracting the execution of a scenario into the executer (which rarely change) and providing an easy way of building scenarios, you can get very rapid feedback into test cycle while maintaining testing at a high level.&lt;/p&gt;

&lt;p&gt;Also, relating to my &lt;a href="http://ayende.com/Blog/archive/2009/09/28/even-tests-has-got-to-justify-themselves.aspx"&gt;previous post&lt;/a&gt;, note what we are testing here isn’t a single class. We are testing the system behavior in a given scenario. Note also that we usually want to assert on various aspects of a single scenario as well (such as in the WillNotUpdateLoginDateOnFailedLogin and WillUpdateBadLoginCountOnFailedLogin tests).&lt;/p&gt;</description><link>http://ayende.com/4218/scenario-driven-tests?Key=de777fb4-fae8-4485-bfc4-e3fa12b57c3b</link><guid>http://ayende.com/4218/scenario-driven-tests?Key=de777fb4-fae8-4485-bfc4-e3fa12b57c3b</guid><pubDate>Tue, 29 Sep 2009 00:46:00 GMT</pubDate></item><item><title>Even tests has got to justify themselves</title><description>&lt;p&gt;Let us get a few things out of the way:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;I am not using TDD. &lt;/li&gt;
    &lt;li&gt;I am not using BDD. &lt;/li&gt;
    &lt;li&gt;I am not using Test After. &lt;/li&gt;
    &lt;li&gt;I am not ignoring testing. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I considered not posting this post, because of the likely response, but it is something that I think it worth at least discussion. The event that made me decide to post this is the following bug:&lt;/p&gt;
&lt;blockquote&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre id="codeSnippet" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; IsValid&lt;br /&gt;{&lt;br /&gt;    get { &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt;.IsNullOrEmpty(Url); }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;p&gt;As you can probably guess, I have an inverted conditional here. The real logic is that the filter is valid if the Url is not empty, not the other way around.&lt;/p&gt;
&lt;p&gt;When I found the bug, I briefly considered writing a test for it, but it struck me as a bad decision. This is code that I don’t see any value in testing. It is too stupid to test, because I won’t have any ROI from the tests.  And yes, I am saying that after seeing that the first time I wrote the code it had a bug.&lt;/p&gt;
&lt;p&gt;The idea behind TDD is to use the tests to drive the design. Well, in this case, I don’t have any design to drive. In recent years, I have moved away from the tenets of TDD toward a more system oriented testing system. &lt;/p&gt;
&lt;p&gt;I don’t care about testing a specific class, I want to test the entire system as whole. I may switch some parts of the infrastructure (for example, change the DB to in memory one), for perf sake, but I usually try to test an entire component at a time. &lt;/p&gt;
&lt;p&gt;My components may be as small as a single class or as big as the entire NH Prof sans the actual UI pieces.  I have posted in the past, showing how I &lt;a href="http://ayende.com/Blog/archive/2009/08/05/nh-prof-new-feature-detect-cross-thread-session-usage.aspx"&gt;implement features for NH Prof&lt;/a&gt;, including the full source code for the relevant sections. Please visit the link, it will probably make more sense to you afterward. It is usually faster, easier and more convenient to write a system test than to try to figure out how to write a unit test for the code.&lt;/p&gt;
&lt;p&gt;Now, let us look at &lt;em&gt;why&lt;/em&gt; people are writing tests:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Higher quality code&lt;/li&gt;
    &lt;li&gt;Safe from regressions&lt;/li&gt;
    &lt;li&gt;Drive design&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Well, as I said, I &lt;em&gt;really&lt;/em&gt; like tests, but my method of designing software is no longer tied to a particular class. I have the design of the class handed to me by a higher authority (the concept), so that is out. Regressions are handled quite nicely using the tests that I do write. &lt;/p&gt;
&lt;p&gt;What about the parts when I &lt;em&gt;am&lt;/em&gt; doing design, when I am working on a new concept?&lt;/p&gt;
&lt;p&gt;Well, there are two problems here:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;I usually try several things before I settle down on a final design. During this bit of churn, it is going to take longer to do things with tests.&lt;/li&gt;
    &lt;li&gt;After I have a design finalized, it is &lt;em&gt;still&lt;/em&gt; easier to write a system level test than write unit tests for the particular implementation.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As a matter of fact, in many cases, I don’t really &lt;em&gt;care&lt;/em&gt; about the implementation details of a feature, I just want to know that the feature works. As a good example, let us take a look at this test:&lt;/p&gt;
&lt;blockquote&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre id="codeSnippet" class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CanGetDurationOfQueries : IntegrationTestBase&lt;br /&gt;{&lt;br /&gt;    [Fact]&lt;br /&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; QueriesSpecifyTheirDuration()&lt;br /&gt;    {&lt;br /&gt;        ExecuteScenarioInDifferentAppDomain&amp;lt;SelectBlogByIdUsingCriteria&amp;gt;();&lt;br /&gt;        var first = model.RecentStatements&lt;br /&gt;            .ExcludeTransactions()&lt;br /&gt;            .First();&lt;br /&gt;        Assert.NotNull(first.DurationViewModel.Inner.Value);&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;p&gt;NH Prof went through three different ways of measuring the duration of a query. The test didn’t need to change. I have a lot of tests that work in the same manner. Specifying the final intent, rather than specifying each individual step.&lt;/p&gt;
&lt;p&gt;There are some parts in which I would use Test First, usually parts that I have high degree of uncertainty about.  The “show rows from query” feature in NH Prof was develop using Test First, because I had absolutely no idea how to approach it.&lt;/p&gt;
&lt;p&gt;But most of the time, I have a pretty good idea where I am and where I am going, and writing unit tests for every miniscule change is (for lack of a better phrase) hurting my style. &lt;/p&gt;
&lt;p&gt;Just about any feature in NH Prof is covered in tests, and we are confident enough in our test coverage to release on &lt;em&gt;&lt;a href="http://ayende.com/Blog/archive/2009/08/21/nh-prof-error-handling-continuous-integration-and-rtm-per-commit.aspx"&gt;every single commit&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;But I think that even a test has got to justify its existence, and in many cases, I see people writing tests that have no real meaning. They duplicate the logic in a single class or method. But that isn’t what I usually care about. I don’t care about what a method or a class does.&lt;/p&gt;
&lt;p&gt;I care about what the overall behavior is. And I shaped my tests to allow me to assert just that. I’ll admit that NH Prof is somewhat of a special case, since you have a more or less central location that you can navigate form which to everything else. In most systems, you don’t have something like that.&lt;/p&gt;
&lt;p&gt;But the same principle remains, if you setup your test environment so you are testing the system, it is going to be much easier to test the system. It isn’t a circular argument. Let us take a simple example of an online shop and wanting to test the “email on order confirmed” feature.&lt;/p&gt;
&lt;p&gt;One way of doing this would be to write a test saying that when the OrderConfirmed message arrive, a SendEmail message is sent. And another to verify that SendEmail message actually send an email.&lt;/p&gt;
&lt;p&gt;I would rather write something like this, however:&lt;/p&gt;
&lt;blockquote&gt;
&lt;div id="codeSnippetWrapper"&gt;
&lt;pre id="codeSnippet" class="csharpcode"&gt;[Fact]&lt;br /&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; WillSendEmailOnOrderConfirmation()&lt;br /&gt;{&lt;br /&gt;    &lt;span class="rem"&gt;// setup the system using an in memory bus&lt;/span&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// load all endpoints and activate them&lt;/span&gt;&lt;br /&gt;    &lt;span class="rem"&gt;// execute the given scenario&lt;/span&gt;&lt;br /&gt;    ExecuteScenario&amp;lt;BuyProductUsingValidCreditCard&amp;gt;();&lt;br /&gt;&lt;br /&gt;    var confirmation = model.EmailSender.EmailsToSend.FirstOrDefault(x=&amp;gt;x.Subject.Contains(&lt;span class="str"&gt;"Order Confirmation"&lt;/span&gt;);&lt;br /&gt;    Assert.NotNull(confirmation);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/blockquote&gt;
&lt;p&gt;I don’t care about implementation, I just care about what I want to assert.&lt;/p&gt;
&lt;p&gt;But I think that I am getting side tracked to another subject, so I’ll stop here and post about separating asserts from their scenarios at another time.&lt;/p&gt;</description><link>http://ayende.com/4217/even-tests-has-got-to-justify-themselves?Key=357db370-53f0-4249-b922-e7c43e35d198</link><guid>http://ayende.com/4217/even-tests-has-got-to-justify-themselves?Key=357db370-53f0-4249-b922-e7c43e35d198</guid><pubDate>Sun, 27 Sep 2009 23:25:00 GMT</pubDate></item><item><title>Assert.True is the tool of last resort</title><description>&lt;p&gt;I was just reviewing someone’s code, and as I was browsing through the tests, I run into the following anti pattern, just about &lt;em&gt;all&lt;/em&gt; the asserts were specified as:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;Assert.True(pet.Owner == "&lt;span style="color: #8b0000"&gt;Baz&lt;/span&gt;");&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;The problem with that is that this is a perfect case of “let’s create a test that will tell us &lt;em&gt;nothing &lt;/em&gt;when it is failing”.&lt;/p&gt;

&lt;p&gt;It seems like a minor thing, to use the more explicit version:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;Assert.Equal("&lt;span style="color: #8b0000"&gt;Baz&lt;/span&gt;", pet.Owner);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;But it isn’t, when this assert fails, we are going to get a &lt;em&gt;lovely&lt;/em&gt; error message telling us &lt;em&gt;exactly&lt;/em&gt; what went wrong. From maintainability standpoint, being able to see why a test failed without debugging it is not a plus, it is &lt;em&gt;essential&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Assert.True is a tool of last resort, don’t use it if you have any choice at all.&lt;/p&gt;</description><link>http://ayende.com/4118/assert-true-is-the-tool-of-last-resort?Key=0ff5e1bd-d159-49d9-8788-9a5041b9ab3f</link><guid>http://ayende.com/4118/assert-true-is-the-tool-of-last-resort?Key=0ff5e1bd-d159-49d9-8788-9a5041b9ab3f</guid><pubDate>Thu, 13 Aug 2009 22:13:00 GMT</pubDate></item><item><title>Fixing test failures using binary search</title><description>&lt;p&gt;With Rhino Queues, I have a very serious problem. While all the tests would run if they were run individually, running them in concrete would cause them to fail. That is a pretty common case of some test affecting the global state of the system (in fact, the problem is one test not releasing resources and all other tests failing because they tried to access a locked resource).&lt;/p&gt;  &lt;p&gt;The problem is &lt;em&gt;finding out &lt;/em&gt;which. By the way, note that I am making a big assumption here, that there is only one test that does so, but for the purposes of this discussion, it doesn’t matter, the technique works for both options.&lt;/p&gt;  &lt;p&gt;At the heart of it, we have the following assumption:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;If you have tests that cause failure, and you don’t know which, start by deleting tests.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;In practice, it means:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Run all unit tests&lt;/li&gt;    &lt;li&gt;If the unit tests failed:&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Delete a unit test file with all its tests&lt;/li&gt;      &lt;li&gt;Go to first step&lt;/li&gt;   &lt;/ul&gt;    &lt;li&gt;If the unit tests passed:&lt;/li&gt;    &lt;ul&gt;     &lt;li&gt;Revert deletion of a unit test file in reverse order to their deletion&lt;/li&gt;      &lt;li&gt;Go to step 1&lt;/li&gt;   &lt;/ul&gt; &lt;/ol&gt;  &lt;p&gt;When you have only a single file, you have pinpointed the problem. &lt;/p&gt;  &lt;p&gt;In my case, unit tests started to consistently pass when I deleted all of those ( I took a shortcut and delete entire directories ):&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Fixingtestfailuresusingbinarysearch_CB9A/image_4.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="91" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Fixingtestfailuresusingbinarysearch_CB9A/image_thumb_1.png" width="230" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;So I re-introduced UsingSubQueues, and… the tests passed.&lt;/p&gt;  &lt;p&gt;So I re-introduced Storage/CanUseQueue and… the tests passed.&lt;/p&gt;  &lt;p&gt;This time, I re-introduced Errors, because I have a feeling that QueueIsAsync may cause that, and… the tests &lt;strong&gt;FAILED&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;We have found the problem. Looking at the Errors file, it was clear that it wasn’t disposing resources cleanly, and that was a &lt;em&gt;very&lt;/em&gt; simple fix.&lt;/p&gt;  &lt;p&gt;The problem wasn’t the actual fix, the problem was isolating it.&lt;/p&gt;  &lt;p&gt;With the fix… the tests passed. So I re-introduced all the other tests and run the full build again.&lt;/p&gt;  &lt;p&gt;Everything worked, and I was on my way :-)&lt;/p&gt;</description><link>http://ayende.com/4014/fixing-test-failures-using-binary-search?Key=25ce68bd-fe9d-4ef8-9f4f-aa387c03873a</link><guid>http://ayende.com/4014/fixing-test-failures-using-binary-search?Key=25ce68bd-fe9d-4ef8-9f4f-aa387c03873a</guid><pubDate>Fri, 15 May 2009 11:07:00 GMT</pubDate></item><item><title>Challenge: multi threaded test failure</title><description>&lt;p&gt;On one of my machines, I can consistently get the following test failure, because of an exception throw here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Challengemultithreadedtestfailure_C55B/image_6.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="661" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Challengemultithreadedtestfailure_C55B/image_thumb_2.png" width="524" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;As you can see, we get an ObjectDisposedException here, which cause the test to fail. There is only one problem with that:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Challengemultithreadedtestfailure_C55B/image_4.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="84" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/Challengemultithreadedtestfailure_C55B/image_thumb_1.png" width="256" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;We only dispose the queue manager on the dispose method, and that can only be called if the test itself was completed.&lt;/p&gt;  &lt;p&gt;On the face of it, it &lt;em&gt;looks &lt;/em&gt;like I am doing things correctly, but there is a bug here. The CLR’s threading semantics are not broken, even though on first glance it appears so.&lt;/p&gt;  &lt;p&gt;As a side note, people keep asking me how I blog so much. This post is a perfect example. When I started writing this blog post, I was truly stamped. By the time I finished, I already noticed where the bug was and fixed it.&lt;/p&gt;  &lt;p&gt;Can you tell?&lt;/p&gt;</description><link>http://ayende.com/4012/challenge-multi-threaded-test-failure?Key=1c9d154d-0be8-4ae3-b084-d92e8838b1f3</link><guid>http://ayende.com/4012/challenge-multi-threaded-test-failure?Key=1c9d154d-0be8-4ae3-b084-d92e8838b1f3</guid><pubDate>Thu, 14 May 2009 10:55:00 GMT</pubDate></item><item><title>Don’t mock my integration tests</title><description>&lt;p&gt;Dror from TypeMock has &lt;a href="http://blog.typemock.com/2009/05/unit-tests-vs-integration-tests.html"&gt;managed to capture&lt;/a&gt; the essence of my post about unit tests vs. integration tests quite well:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Unit tests runs faster but integration tests are easier to write.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Unfortunately, he draws an incorrect conclusion out of that;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;There is however another solution instead of declaring unit testing as hard to write - use an isolation framework which make writing unit test much easier. &lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;And the answer to that is… no, you can’t do that. Doing something like that put you back in the onerous position of unit test, where you actually have to understand exactly what is going on and have to deal with that. With an integration test, you can assert directly on the end result, which is completely different than what I would have to do if I wanted to mock a part of the system. A typical integration test looks something like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;div&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SelectNPlusOne : IScenario
{
    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Execute(ISessionFactory factory)
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var session = factory.OpenSession())
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var tx = session.BeginTransaction())
        {
            var blog = session.CreateCriteria(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Blog))
                .SetMaxResults(1)
                .UniqueResult&amp;lt;Blog&amp;gt;();&lt;span style="color: #008000"&gt;//1&lt;/span&gt;
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var post &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; blog.Posts)&lt;span style="color: #008000"&gt;//2&lt;/span&gt;
            {
                Console.WriteLine(post.Comments.Count);&lt;span style="color: #008000"&gt;// SELECT N&lt;/span&gt;
            }
            tx.Commit();
        }
    }
}


[Fact]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllCallsToLoadCommentsAreIdentical()
{
    ExecuteScenarioInDifferentAppDomain&amp;lt;SelectNPlusOne&amp;gt;();
    var array = model.Sessions[0]
        .SessionStatements
        .ExcludeTransactions()
        .Skip(2)
        .ToArray();
    Assert.Equal(10, array.Length);
    &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (StatementModel statementModel &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; array)
    {
        Assert.Equal(statementModel.RawSql, array[0].RawSql);
    }
}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;This shows how we can assert on the actual end model of the system, without really trying to deal with what is actually going on. You cannot introducing any mocking to the mix without significantly hurting the clarity of the code.&lt;/p&gt;</description><link>http://ayende.com/4008/don-t-mock-my-integration-tests?Key=2f5fb5f8-3627-4356-9d38-2b4eb92c2231</link><guid>http://ayende.com/4008/don-t-mock-my-integration-tests?Key=2f5fb5f8-3627-4356-9d38-2b4eb92c2231</guid><pubDate>Tue, 12 May 2009 23:26:59 GMT</pubDate></item><item><title>Unit tests vs. Integration tests</title><description>&lt;p&gt;I can sum it up pretty easily:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Unit tests:      &lt;br /&gt;65 passed, 0 failed, 5 skipped, took 7.49 seconds. &lt;/p&gt;    &lt;p&gt;Integration tests:     &lt;br /&gt;45 passed, 0 failed, 0 skipped, took 284.43 seconds.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;em&gt;Except&lt;/em&gt;, there is one thing that you aren’t seeing here. Writing an integration tests is about as straightforward as they come. Execute the user scenario, assert the result. There is no real knowledge required to &lt;em&gt;write the test&lt;/em&gt;, and they are very easy to read and understand.&lt;/p&gt;  &lt;p&gt;Unit tests, however, often require you to understand how the software work in much finer detail, and they often make no sense to someone who isn’t familiar with the software.&lt;/p&gt;</description><link>http://ayende.com/4003/unit-tests-vs-integration-tests?Key=d083fb79-02e9-4235-a76e-33ea71326ef9</link><guid>http://ayende.com/4003/unit-tests-vs-integration-tests?Key=d083fb79-02e9-4235-a76e-33ea71326ef9</guid><pubDate>Sun, 10 May 2009 12:14:00 GMT</pubDate></item><item><title>NHibernate Unit Testing</title><description>&lt;p&gt;When using NHibernate we generally want to test only three things, that properties are persisted, that cascade works as expected and that queries return the correct result. In order to do all of those, we generally have to talk to a real database, trying to fake any of those at this level is futile and going to be very complicated.&lt;/p&gt;  &lt;p&gt;We can either use a standard RDBMS or use an in memory database such as SQLite in order to get very speedy tests.&lt;/p&gt;  &lt;p&gt;I have a pretty big implementation of a base class for unit testing NHibernate in Rhino Commons, but that has so many features that I forget how to use it sometimes. Most of those features, by the way, are now null &amp;amp; void because we have &lt;a href="http://nhprof.com/"&gt;NH Prof&lt;/a&gt;, and can easily see what is going on without resorting to the SQL Profiler. &lt;/p&gt;  &lt;p&gt;At any rate, here is a very simple implementation of that base class, which gives us the ability to execute NHibernate tests in memory.&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; InMemoryDatabaseTest : IDisposable
{
	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Configuration Configuration;
	&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; ISessionFactory SessionFactory;
	&lt;span style="color: #0000ff"&gt;protected&lt;/span&gt; ISession session;

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; InMemoryDatabaseTest(Assembly assemblyContainingMapping)
	{
		&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Configuration == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
		{
			Configuration = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Configuration()
				.SetProperty(Environment.ReleaseConnections,"&lt;span style="color: #8b0000"&gt;on_close&lt;/span&gt;")
				.SetProperty(Environment.Dialect, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (SQLiteDialect).AssemblyQualifiedName)
				.SetProperty(Environment.ConnectionDriver, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(SQLite20Driver).AssemblyQualifiedName)
				.SetProperty(Environment.ConnectionString, "&lt;span style="color: #8b0000"&gt;data source=:memory:&lt;/span&gt;")
				.SetProperty(Environment.ProxyFactoryFactoryClass, &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (ProxyFactoryFactory).AssemblyQualifiedName)
				.AddAssembly(assemblyContainingMapping);

			SessionFactory = Configuration.BuildSessionFactory();
		}

		session = SessionFactory.OpenSession();

		&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SchemaExport(Configuration).Execute(&lt;span style="color: #0000ff"&gt;true&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;, session.Connection, Console.Out);
	}

	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Dispose()
	{
		session.Dispose();
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;This just set up the in memory database, the mappings, and create a session which we can now use. Here is how we use this base class:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; BlogTestFixture : InMemoryDatabaseTest
{
	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; BlogTestFixture() : &lt;span style="color: #0000ff"&gt;base&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(Blog).Assembly)
	{
	}

	[Fact]
	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CanSaveAndLoadBlog()
	{
		&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; id;

		&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var tx = session.BeginTransaction())
		{
			id = session.Save(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Blog
			{
				AllowsComments = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;,
				CreatedAt = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DateTime(2000,1,1),
				Subtitle = "&lt;span style="color: #8b0000"&gt;Hello&lt;/span&gt;",
				Title = "&lt;span style="color: #8b0000"&gt;World&lt;/span&gt;",
			});

			tx.Commit();
		}

		session.Clear();


		&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (var tx = session.BeginTransaction())
		{
			var blog = session.Get&amp;lt;Blog&amp;gt;(id);

			Assert.Equal(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DateTime(2000, 1, 1), blog.CreatedAt);
			Assert.Equal("&lt;span style="color: #8b0000"&gt;Hello&lt;/span&gt;", blog.Subtitle);
			Assert.Equal("&lt;span style="color: #8b0000"&gt;World&lt;/span&gt;", blog.Title);
			Assert.True(blog.AllowsComments);

			tx.Commit();
		}
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Pretty simple, ah?&lt;/p&gt;</description><link>http://ayende.com/3983/nhibernate-unit-testing?Key=089097a5-dc15-4e1f-bf76-47c8a9e6b69f</link><guid>http://ayende.com/3983/nhibernate-unit-testing?Key=089097a5-dc15-4e1f-bf76-47c8a9e6b69f</guid><pubDate>Tue, 28 Apr 2009 06:32:00 GMT</pubDate></item><item><title>Relying on hash code implementation is BAD – part II</title><description>&lt;p&gt;To be truthful, I never thought that I would have a following for &lt;a href="http://ayende.com/Blog/archive/2005/06/16/ProgrammersTricksAreNastyAndCruel.aspx"&gt;this post&lt;/a&gt; 4 years later, but I run into that today.&lt;/p&gt;  &lt;p&gt;The following is a part of an integration test for NH Prof:&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;Assert.AreEqual(47, alerts[&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StatementAlert(&lt;span style="color: #0000ff"&gt;new&lt;/span&gt; NHProfDispatcher())
{
	Title = "&lt;span style="color: #8b0000"&gt;SELECT N+1&lt;/span&gt;"
}]);&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;I am reviewing all our tests now, and I nearly choked on that one. I mean, who was stupid enough to write code like this?  I mean, yes, I can understand what it is doing, sort of, but only because I have a dawning sense of horror when looking at it.&lt;/p&gt;

&lt;p&gt;I immediately decided that the miscreant that wrote that piece of code should be publically humiliated and  chewed on by a large dog.&lt;/p&gt;

&lt;p&gt;SVN Blame is a wonderful thing, isn’t it?&lt;/p&gt;

&lt;p&gt;&lt;a href="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/RelyingonhashcodeimplementationisBADpart_F924/image_2.png"&gt;&lt;img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="204" alt="image" src="http://ayende.com/Blog/images/ayende_com/Blog/WindowsLiveWriter/RelyingonhashcodeimplementationisBADpart_F924/image_thumb.png" width="726" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Hm… there is a problem here.&lt;/p&gt;

&lt;p&gt;Actually, there are a couple of problems here. One is that we have a pretty clear indication that we have a historic artifact here. Just look at the number of version that are shown in just this small blame window. This is good enough reason to start doing full fledged ancestory inspection. The test has started life as:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;[TestFixture]
&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; AggregatedAlerts:IntegrationTestBase
{
	[Test]
	&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Can_get_aggregated_alerts_from_model()
	{
		ExecuteScenarioInDifferentAppDomain&amp;lt;Scenarios.ExecutingTooManyQueries&amp;gt;();

		var alerts = observer.Model.Sessions[1].AggregatedAlerts;
		Assert.AreEqual(47, alerts["&lt;span style="color: #8b0000"&gt;SELECT N+1&lt;/span&gt;"]);
		Assert.AreEqual(21, alerts["&lt;span style="color: #8b0000"&gt;Too many database calls per session&lt;/span&gt;"]);
	}
}&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which I think is reasonable enough. Unfortunately, it looks like somewhere along the way, someone had taken the big hammer approach to this. The code now looks like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;pre&gt;Assert.AreEqual(47, alerts.First(x =&amp;gt; x.Key.Title == "&lt;span style="color: #8b0000"&gt;SELECT N+1&lt;/span&gt;"));&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Now &lt;/em&gt;this is readable.&lt;/p&gt;

&lt;p&gt;Oh, for the nitpickers, using hash code evaluation as the basis of any sort of logic is &lt;em&gt;wrong&lt;/em&gt;. That is the point of this post. It is a non obvious side affect that &lt;em&gt;will&lt;/em&gt; byte* you in the ass.&lt;/p&gt;

&lt;p&gt;* intentional misspelling&lt;/p&gt;</description><link>http://ayende.com/3981/relying-on-hash-code-implementation-is-bad-part-ii?Key=99844bbf-4490-482a-95a6-60f8cda7da36</link><guid>http://ayende.com/3981/relying-on-hash-code-implementation-is-bad-part-ii?Key=99844bbf-4490-482a-95a6-60f8cda7da36</guid><pubDate>Mon, 27 Apr 2009 14:43:11 GMT</pubDate></item></channel></rss>