Today was a good day. I was given a new project at work and in the spec that had been written up there was a comment that the functionality was fairly complex and so a test plan should be written up. This screamed to me as being the first chance that I might have to be able to introduce TDD practices in my workplace, and things went rather well on that front. My boss vaguely recognised the name of NUnit, but knew nothing about it. I explained the general principal of having a suite of consistent tests attached to the code that can be run quickly and easily to make any breaking changes instantly obvious and he was happy for me to give it a shot.
The only concern that he expressed to me was that of cost. Our department is what could be described as desperately underfunded. Developers are expected to provide a certain amount of IT support to the rest of the company, we are still using SQL Server 2000 for production databases, and we only have the free Express editions of the developer tools to work with. Anything that costs money is not going to get the go-ahead because we have none to spend. Fortunately there is some very good open source software in this area, so permission was granted.
In my investigations into TDD at home I have become accustomed to being able to run my tests in the IDE by way of tools like Resharper or TestDriven.Net. This was something that I was keen to continue with as having to switch back and forth between the IDE and the NUnit GUI is an added distraction, but the Express tools don't allow you to use any plugins. Fortunately I have recently been raiding the back catalogue of the Hanselminutes podcast and remembered Scott mentioning some open source tools that had sprung up around the Mono project, including an IDE called SharpDevelop. This has an integrated NUnit test runner, so I installed it on my development box and was off.
The next step in being able to do effective TDD was to get a mocking framework in place. I have had some experience with Moq at home on C# projects, but the reason that I chose it was because it is very specifically tuned for C# and the language of choice in my current job is VB.Net. Some googling indicated that it may be a little more tricksy to use effectively with this language restriction, in fact it seemed like all of the frameworks with the possible exception of TypeMock are C# focused to the detriment of VB.Net. I ended up deciding to use Rhino Mocks as there seems to be a lot of information on the web regarding it and because the .Net community has a lot of respect for Oren Eini so I feel happy using anything that he is involved in. With my IDE integrated test runner and my mocking framework in place I was ready to get started, and despite it being a bit of a slow start as I had a bit to learn about my new toolkit I am happy with the results of my day's work.
The biggest stumbling block that I have encountered with this set-up so far came up when I started my first mock. I was setting up a mock of my repository so that the business logic could receive a result for a data request. Using the Arrange, Act, Assert (or AAA) syntax, I needed to call Stub on my object. This is an extension method added by Rhino Mocks used to program the mock object so that it can respond as desired to certain requests made of it, and it takes an argument in the form of lambdas pointing to the method that will be called. For some reason this wouldn't compile. I was given a very short error message by the compiler stating that there was an issue with the Stub method, but it was not at all helpful. I spent a while trawling the internet for clues but to no avail. I finally decided to try compiling it in Visual Studio instead of SharpDevelop, I wasn't very hopeful about what the outcome would be as SharpDevelop will still be using the same MSBuild to compile it but it remained one of few options left open to me. Unsurprisingly I had the same error, but with one very important difference. The error message was MUCH longer. I can't remember the exact detail, but the essence of it was that I had some error in the construction of my lambda in the Stub method. SharpDevelop merely told me the outermost part was failing at compilation, but Visual Studio was giving me the trace of what was going on within it and so I was able to spot and fix the error pretty quickly. I switched back to using SharpDevelop at this point so that I could continue to use the test runner.
From what I have seen of SharpDevelop so far, it is not complete enough to entirely replace having a Microsoft IDE on my system, and this could become more of an issue with ASP.Net sites as the support for this is meant to be minimal and most of my work calls for ASP. However, it offers compelling reasons to use it alongside the MS toolset when restricted to just using free software. I'm happy to be able to stick to the full version of Visual Studio with the personal version of TestDriven.net at home, and having played with trial versions of Resharper and CodeRush & Refactor I can see how these could add a lot to my productivity which SharpDevelop will never be able to match, but it has certainly helped things along today and I hope to get more out of it in the future, especially as that would imply that the company is happy for me to keep on using a TDD approach.