I’m slowly working through my backlog of open source pet-projects. One that I worked on quite a while ago was called NHashcash, a managed .NET implementation of the anti-spam mechanism, Hashcash. Hashcash hasn’t been broadly adopted as far as I can tell but I thought it would be interesting to make the library available via NuGet anyway.
You can install the NHashcash library by issuing the following command on the NuGet package manager console:
The screenshot below highlights this command and the assembly that it adds to the currently selected project references. The sample code is just there to give you an overall taste for the API (its very simple to use).
If you are interested in the source code you can grab it from the project portal on CodePlex. The 1.1.0 version of NHashcash has a major bug fixed there the timestamp used for stamps was incorrectly being generated. I guess its a testament to how few people use Hashcash that the bug was never found, and also how poor my initial unit tests were.
On the unit testing front I’ve improved the tests somewhat to validate against the reference implementation of Hashcash (which was how I found the bug). I also used the MSTestContrib library that I blogged about a few days ago to make the tests more BDD-style.
Finally, there is more work to be done around performance on NHashcash since it was never an optimal implementation. Anyway – if you can’t use NuGet you can always download it directly from here.
Late last night I uploaded the RTM release of MSTestContrib 1.0.0 to the MSTestContrib project portal up on CodePlex. This release of MSTestContrib includes the ability to write BDD-style unit tests simply by sub-classing MSTestContrib.Specifications.Specification and decorating the class and methods with a few attributes.
The easiest way to get going with MSTestContrib is to install it using the NuGet package manager. If you have ASP.NET MVC 3.0 RC2 or above installed you already have NuGet installed. On the NuGet console issue the command “install-package MSTestContrib” with the unit test project selected as the default project.
If you don’t have access to NuGet you can still download a zip file that contains just the MSTestContrib DLL from the project portal releases page. The MSTestContrib.dll file needs to be referenced by the unit test project. Once this is setup you can create a new test class as per the normal process and then sub-class MSTestContrib.Specifications.Specification and add some attributes. The test method implementation just uses a series of methods to build up the BDD-style statement. The following code sample (gist 760628) shows a specification without any implementation detail provided.
Running this test using MSTest will produce an inconclusive result. The result summary view functions as you would normally expect.
Drilling in a bit closer to the individual test result you can see that MSTestContrib has reproduced a textual representation of the BDD-style test, spelling out the specification description, scenario description and the various BDD statements. At the end of each statement there is a “NotImplemented” indicator which is the cause of the inconclusive test result.
The following code sample (gist 760641) shows a completed test specification including not only the plain text description of the test case but also a function/expression that implements that description.
If the Add(…) method on the calculator threw an exception then the test result output would provide not only the text-based representation of the specification but also all of the exception detail. The following screenshot provides an example of the output.
It’s not pretty but there are limits to how much formatting you can put into the error message within the MSTest framework.
MSTestContrib Goals and Non-Goals
I wrote MSTestContrib primarily as a BDD-framework that I was happy using. There are dozens of BDD frameworks out-there including some really great ones. I was inspired by StoryQ but I wanted something that was simpler still. By taking dependencies on MSTest (the out of the box unit testing framework that comes with Visual Studio 2010) I hope to reduce barriers to entry for developers who have not done BDD-style development before.
Over time I am hoping that MSTestContrib will be extended with other framework elements which support teams that are investing in MSTest. If Microsoft produces tooling in MSTest which makes elements of MSTestContrib redundant then those features will be depreciated.
After playing around with it a little bit I decided that it would be handy if the toolkit was actually contained in a NuGet Package. So I created a fork and pushed it into the temporary NuGet Package repository. The package has now been accepted and is available from the package manager console in Visual Studio 2010 (with NuGet installed, or ASP.NET MVC 3.0 or above).
The first thing you need to do is open up your ASP.NET application inside Visual Studio 2010. The package assumes that you are using ASP.NET MVC, but to be honest it’ll work just fine with plain-old ASP.NET as well (except that the samples will be located under /Content/JitExamples/).
The package contains three script files (jit.js, jit-yc.js, excancas.js) and a number of samples (under /Content/JitExamples). Once you are comfortable with how the toolkit works you can simply delete the examples.
If you want to get a feel for what the package can do, set the various HTML files under JitExamples to your start page and hit F5. Enjoy!