Sunday, September 30, 2007

I have been using MbUnit's AutoRunner a lot lately and it rocks. To use the AutoRunner add a new class to your test project named SelfTester with the following code:

public class SelfTester
{
    public static void Main()
    {
        using(AutoRunner runner = new AutoRunner())
        {
            runner.Load();
            runner.Run();
            runner.ReportToHtml();
        }
    }
}

Next right click on your test project and set it as the start up project. Change the output type to Console Application and the startup object to your SelfTester class:

SelfTester

 Now hit F5 and your test results will appear in a nicely formatted html page. This works even if all your tests are written using NUnit as the framework. It does not however work with MSTest.

 

Now Playing: The Chemical Brothers - We Are The Night - All Rights Reversed

Sunday, September 30, 2007 6:17:03 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 

One problem a lot of people face is not being able to test static methods. I am curious as to what other people may be using but this is a technique that I have been using that is fairly effective. I have to point out that I first found this technique in Ayende's Rhino Commons.

To demonstrate this techinque for testing static methods assume we have a class named Assert that has two methods in it IsTrue and Fail. It might look like this:

public sealed class Assert
{
    static public void IsTrue(bool condition, string message)
    {
        if(!condition)
            Fail(message);
    }
 
    static public void Fail(string message)
    {
        throw new AssertionException(message);
    }
}

To test this class we can create an instance class that is a mirror image of the original class without the static keyword:

public sealed class Asserter
{
    public void IsTrue(bool condition, string message)
    {
        if (!condition)
            Fail(message);
    }
 
    public void Fail(string message)
    {
        throw new AssertionException(message);
    }
}

Now we are able to extract an interface from the newly created class:

public interface IAssert
{
    void IsTrue(bool condition, string message);
    void Fail(string message);
}

Next we add a private static getter to the original class that is resolved using Windsor Container and change the static methods to delegate to the private getter.

public sealed class Assert
{
    private static IAssert InternalAssert
    {
        get
        {
            IWindsorContainer container = new WindsorContainer(new XmlInterpreter());
            return container.Resolve<IAssert>();
        }
    }
 
    static public void IsTrue(bool condition, string message)
    {
        InternalAssert.IsTrue(condition, message);
    }
 
    static public void Fail(string message)
    {
        InternalAssert.Fail(message);
    }
}

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section
            name="castle"
            type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
    </configSections>
    <castle>
        <components>
            <component
                id="assert"
                service="MbUnit.Framework.IAssert, MbUnit.Framework"
                type="MbUnit.Framework.Asserter, MbUnit.Framework" />
        </components>
    </castle>
</configuration>

We can now introduce a class that we can inject a Inversion of Control container into, this allows us to mock out the container at test time:

public class IocContainer
{
    private static IWindsorContainer container;
 
    public IocContainer(IWindsorContainer container)
    {
        IocContainer.container = container;
    }
 
    public static IWindsorContainer Container
    {
        get { return container; }
    }
}

We also need to replace the getter in the static class with our newly created IocContainer class:

private static IAssert InternalAssert
{
    get
    {
        return IocContainer.Container.Resolve<IAssert>();
    }
}

Our test for the static Assert class now looks like this:

[Test]
public void ShouldAssertConditionIsTrue()
{
    MockRepository mockery = new MockRepository();
 
    IWindsorContainer iocContainer = mockery.CreateMock<IWindsorContainer>();
    IocContainer container = new IocContainer(iocContainer);
 
    IAssert assert = mockery.CreateMock<IAssert>();
    using(mockery.Record())
    {
        Expect.Call(IocContainer.Container.Resolve<IAssert>()).Return(assert);
        assert.IsTrue(1 == 2, "One does not equal Two");
    }
    using(mockery.Playback())
    {
        Assert.IsTrue(1==2, "One does not equal Two");
    }
}
Sunday, September 30, 2007 5:42:34 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 
Sunday, September 23, 2007
I've started a side blog at http://my.hockeybuzz.com/knucklesandwich. It is all about hockey and it came with my subscription to Hockey Buzz. My first post is my first impressions of the Flyers/Rangers preseason game that I watched today. To summarize it here, the Flyers off-season moves were a lot better than the Rangers off-seasons moves (the Rangers defense looked like pylons in blue jerseys).

If you are interested in hockey please check it out and give me some feedback.
Sunday, September 23, 2007 3:00:26 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 
Monday, September 10, 2007

Dear Friend,

Just because WestJet does not give away headsets for free doesn't mean you should feel obligated to provide commentary to the touranment on the golf channel.

Thank you,

Steven

Monday, September 10, 2007 5:21:31 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 

This year my fiancee Catherine and I will once again be participating in the SuperWalk for Parkinson's (SPONSOR US HERE). Last year I lost my Grandfather to Parkinson's disease.

Sir_William_Richard_Gowers_Parkinson_Disease_sketch_1886

This year I am walking in memory of my Grandfather, but I am also walking for my auntie who has shown a lot of courage while dealing with Parkinson's Disease. Imagine drinking 20 cups of coffee everyday and then trying to knit a quilt, it can get very frustrating really fast trying to keep your hands steady enough to sew a straight line. A couple of years ago my auntie knit quilts for the whole family (10 of us) while suffering from the effects of the disease which included tremors in her hands.

Today, my auntie is now able to enjoy a better lifestyle because of treatment from drugs and an operation which is all possible because of the money raised from the SuperWalk. Her strength and perseverance has been an inspiration to me. If you are interested please sponsor Catherine and I in this year's SuperWalk so we can continue to raise money for Parkinson's research.

Monday, September 10, 2007 5:12:41 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 
Tuesday, September 04, 2007

Setting up TextMate to talk to DasBlog is fairly easy. Here are the steps I used to get them working together:

  1. In TextMate go to Bundles->Blogging->Setup Blogs
  2. Add a title for your blog name (mine is stevenrockarts) and add the URL to the metaweblog API that DasBlog exposes (mine is http://www.stevenrockarts.com/blog/blogger.aspx)
  3. Go to Bundles->Blogging->Fetch Post you will be prompted for your blog username and password. If you are successful TextMate will retrieve your last post.

Tuesday, September 04, 2007 6:13:23 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 
Friday, August 31, 2007

Check out this billboard that the Dallas Stars are using to advertise hockey. As a Canadian, I don't care if Americans watch hockey but it is nice to see hockey take a shot back at some of the other sports after all the shots taken at hockey from American "sports experts".

Friday, August 31, 2007 5:22:37 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 
Friday, August 24, 2007

Registration for the Edmonton Code Camp on October 20th is now open! You can sign up here: http://tinyurl.com/2v9pe5 

One of the best things you can do for your career in software development is to invest time in learning new things. If you take 8 hours out of one weekend you will be rewarded when you go to work the following Monday. Most of the presenters that come to code camp try their best to give you something that you can use in the real world.

We are also still looking for some speakers, if you have never spoken in front of an audience before I encourage you to try it out at code camp. Personally I find it a lot easier to talk about something I am familiar with like software development in front of a crowd.  

There also seems to be the perception that code camps are centered entirely around Microsoft technologies, this perception is totally wrong! If you want to come talk about Ruby, Java, Python, Lisp, Erlang, or Haskell feel free to submit a session. We talked about Ruby and the RubyCLR last year and according to inside sources it was what inspired Microsoft to make IronRuby.

Friday, August 24, 2007 8:09:54 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 
Monday, August 13, 2007

I have been using VSTSTest on my current project for around 6 months now and I find that it causes a lot of pain and it has a lot of hacky quirks. I'm not the only one the doesn't like it.

I have decided to see how much effort it would take to switch to an MbUnit/NCover combination here at work.

Using ReSharper I first analyze usages of the Microsoft.VisualStudio.TestTools.UnitTesting namespace. 108 usages, it's everywhere! :(

The first step is going to be to change all of the attributes from the VSTS [TestClass] and [TestMethod]. If Microsoft would have named these according to all the other unit testing suites that came before it I could've skipped this step. Here are the attributes I had to change:

VSTS Attribute MbUnit Attribute
[TestClass] [TestFixture]
[TestInitialize] [SetUp]
[TestCleanup] [TearDown]
[TestMethod] [Test]
[DeploymentItem] Change the resource to copy to the output dir

 

The next problem that I came across was that our team had a bunch of tests that were testing private methods. For those that don't know VSTSTest generates a file called VSCodeGenAccessors in your test project when you (sigh) right click on a private method and select Create Private Accessor. MbUnit has the ability to test private methods so I had to change all of the tests that used the private method testing to use the MbUnit equivalent:

Reflector.Invoke(objectUnderTest, "PrivateMethod", "parameters");

Now, I am in a compiling state! I run the tests and they all pass. I am free of the shakles!

Monday, August 13, 2007 10:54:03 PM (GMT Standard Time, UTC+00:00)  #    Comments [5]  | 
Friday, August 03, 2007

Did you know that using the File Open dialog in windows you can do a lot of the same things you can do on the command line? For example, to go up a directory type .. into the File name textbox:

filename

If there are a lot of files inside of the directory you can filter it by typing in *.extension. Like this directory that has a lot of files if I want to quickly find the project file I can type in *.csproj and get a filtered view of all the files:

filteredView

If you have any other obscure tips like this let me know in the comments.

Friday, August 03, 2007 8:13:43 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]  | 

Theme design by Jelle Druyts

Pick a theme: