I really like how BDD specs read, especially in the test result window. We dont use anything like MSpec yet. Im still trying to get testing to gain momentum and making them look as far from regular code as MSpec does makes it that much more difficult.

We do have a pattern we follow:

public class When_something_happens : Specification{

  Foo somethingUnderTest;
  Bar result;

  override public void Given(){
    somethingUnderTest = new Foo(...);
  }

  override public void When(){
    result = somethingUnderTest.doesSomething();
  }

  [Then] public void the_result_should_not_be_null(){
     result.ShouldNotBeNull();
  }

}

 

We just created a base Specification, renamed the xUnit FactAttribute to ThenAttribute (probably unnecessary) and then created a load of extensions of object for the ShouldBeWhatevers. There is more noise than there would be with MSpec, but that's the tradeoff for looking like code.

Im pretty happy with all this and it works pretty well, but what Im finding is that the BDD specs I write before I write a class often dont test what the class does in the end, after that work has been off loaded to a dependency. Take for example the following simple spec. (bits have been left out for simplicityknow it wont compile)

public class When_creating_an_order_invoice_view_model_from_an_order : Specification {

  override public void Given(){
    factory = new OrderInvoiceViewDataFactory();
    order = new Order{ FirstName = "Homer",
                       LastName  = "Simpson",
                       Items     = new []{
                                     new LineItem{ Product ="Duff 6 pack",
                                                            Price = 4.99m, }
                                     new LineItem{ Product ="Duff can koozie",
                                                            Price = 0.99m, }
                                   }
  }

  override public void When(){
    invoice = factory.BuildFrom(order);
  }

  [Then] public void the_total_should_be_the_sum_of_the_items(){
    invoice.Total.ShouldBe(4.99m + 0.99m);
  }

}

 

Overlooking the simplicity of the spec, it seems reasonable right?

Heres my problem. I build my factory to calculate the total, but as soon as I do I realize that totaling the order in the factory that builds the view data violates SRP.

Thats easy enough to fix. I just end up with a factory that looks like this:

public class OrderInvoiceViewDataFactory{
  public OrderInvoiceViewDataFactory(IOrderTotalCalculator calculator){
    //...
  }
}

 

Now I mock that out in my spec:

public class When_creating_an_order_invoice_view_model_from_an_order : Specification {
  override public void Given(){
    // ..

    mockCalculator = new Mock<(IOrderTotalCalculator>();
    mockCalculator
      .Setup(x=>x.CalculateTotal(order))
      .Returns(3.14m);
    factory = new OrderInvoiceViewDataFactory(mockCalculator.Object);

  }

  [Then] public void the_total_should_be_the_sum_of_the_items(){
    // we only need to test what out mock returns
    invoice.Total.ShouldBe(3.14m);
  }

}

 

and abracadabramy spec tests nothing.

From a spec perspective its valid, but from a test perspective, all it tests is that my mocking framework works and Im making that call to it like I expect.

So what do you do? All the test around the calculations move to another file and the spec  gets simplified to just test the interaction with the dependencies?

[Then] public void the_calculator_was_called_to_calculate_the_total(){
   mockCalculator.Verify(x=>x.CalculateTotal(order),Times.Once);
}

 

What I loose here is my nice test result that anyone can look at an understand. All my non-techy folks need to look at several different places to see if that order invoice is be calculated correctly.

You could make this an integration test and test the calculator as well, but I dont like that idea either.

This is probably a sign that Im not writing my specs correctly or I should identify this is going to be factored out sooner. This hasnt been the case so far. Especially when we are translating something from a graph of objects to a flat representation  for a model in our MVC app, a lot of the work ends up being factored out or pushed into AutoMapper where we need to write a different tests for it.

When we are done with the refactoring, the original spec for the class is very skinny, which is good, but as I said, seeing the results of how the story is progressing is more difficult. Maybe that is where my focus needs to be. How do you tie the specs to a specific story and see the results in an aggregated manner?

The Problem

So our problem is we are building a library of JavaScript files that we use all over the place. Ive been pushing, on the project where Im the sole developer, to wrote 99% of the JavaScript as jQuery plugins. This works great because we can compile that 99% into a single file, or to reverse the perspective, we can split that huge file into smaller, more manageable and logical files.

For example, we have a file called jquery.mycompany.core.js which does a variety of mundane tasks. The file is huge, but unlike C# code, its huge for a reason. We want the browser to make as few requests as possible so there is less handshaking, less overhead, and faster page loads.

Previously we had several files and we manually combine them to one and then run it through some online compressing tool once we get close to production. This technically worked, but changes found a way of making their way into the huge file and the segmented files become out of date and obsolete.

The Hack

So here was my plan, take a list of file, concatenate them, and then compress them. Simple enough right? Well, it actually isnt that bad

  <ItemGroup>
    <JavaScriptFiles  Include="scripts\jQueryPlugins\Custom\core*\.js" />
  </ItemGroup>

<Target Name="Default" DependsOnTargets="BuildAndCompressOrdersJavaScript">

</Target>

<Target Name="BuildAndCompressJavaScript"> <ReadLinesFromFile File="%(JavaScriptFiles.Identity)" > <Output TaskParameter="Lines" ItemName="lines"/> </ReadLinesFromFile>

&lt;WriteLinesToFile File=&quot;scripts\jquery.mycompany.core.uncompressed.js&quot;
                  Lines=&quot;@(Lines)&quot;
                  Overwrite=&quot;true&quot; /&gt;

</Target>

Not so bad right? Lastly, just add a bit of Google Closure. I just shell out and run it from the command line here.

<Exec Command='java -jar ..\compiler.jar --js scpts\jq.myco.core.un.js > scpts\jq.myco.core.js' />

I shortened some paths and filenames there so it would fit better in the blogs column, but you get the idea.

I threw this into a post build event even though I could have added it to the csproj directly. The post build event was easier and it avoids those nasty warnings when you open the csproj.

The only issue I see with it right now is it is not as portable as I would like it to be. You pretty much company and past the build steps if you need to use it again. This is my major gripe about MSBuild. Its difficult to create functions you repeat in the build.

This is a total cheapskate way to accomplish this task. I should be creating a nice parameterized MSBuild task, or even better, dump MSBuild for Rake. MSBuild and I acknowledge we are never going to be friends and have come to a professional arrangement. MSBuild does nothing to accommodate me and I do as little as possible with it.

madmen_icon

Working on the bleeding edge is painful. Very painful at times. Throwing away our Team City builds because TFS 2010 didnt support it really hurt. It was a big woops that I regret.

Moving to Visual Studio 2010 Beta was painful too. Visual Studio worked fine, but no ReSharper made it painful. Even in the early days, ReSharper was not stable in 2010, but after a few months, things look good now. ReSharper and VS 2010 are playing nice together.

There is one last pain point. No Xunit.net test runner. Xunitcontrib provides a ReSharper 5.0 build, but I was never able to get it to workuntil now. Previously I was getting the same problem seen by others.

The plugin xunit could not be loaded from "C:\program files(x86)\jetBrains\resharper\v5.0\bin\plugins\xunit.dll" or one of its dependencies.  Operation is not supported.  (Exception from HRRESULT: 0x801311515)

I tried the best I could to right-click | Properties | Unblock all the files in the plug-in, but the Unblock never worked. I would get the same error. Going back into the properties dialog, the unblock would appear again. It looked like the unblock wasnt sticking.

After a quick Google search, I found an alternative way to unblock files. Copy the files to a FAT 32 and back. The block is stripped. Sweet! My USB key is FAT 32, and after a quick copy and back. No more errors! ReSharper sees and runs my Xunit.net tests.

Just a quick overview of how I installed the plug-in:

  1. Close Visual Studio.
  2. Copy the contents of the ExternalAnnotations to:
    C:\Program Files (x86)\JetBrains\ReSharper\v5.0\Bin\ExternalAnnotations
  3. Create a folder:
    C:\Program Files (x86)\JetBrains\ReSharper\v5.0\Bin\plugins\xunitcontrib.runner.resharper.5.0
  4. Copy everything else to the USB key.
  5. Copy it to the folder I just created in step #3.
  6. Open Visual Studio and run all my tests.

I have to admit, I havent used Visual Studio 2010 much, and only recently have I made an effort to switch to it as my daily editor. One thing Ive noticed is the font rendering in Visual Studio 2010 is a bit bolder than in 2008.

Here we have Visual Studio 2008 on the left and Visual Studio 2010 RC on the right. Both using similar font settings (Monaco 10pt)

image

Its easy to see how the fonts in 2010 (on the right) look bolder and a bit blurry. From what Ive read, WPF has had problem with Clear Type font rendering. This is the first WPF application I use with any appreciation. 

Windows 7 has a tool to help you tune your Clear Type settings. Just type clear type into the start search bar, and you should find the tool. It only takes a few moments, and the outcome was pretty surprising. Ive very happy with the results after the calibration.

image

Its been a while since Ive posted and Im trying to get back into the swing of things again. I dont really have a lot to say about the problem, just yet. Weve identified some breaking changes with ASP.Net MVC 2.0 RC 2.

RC 2 switches to relying on all data annotations on the model when doing validation, when previous releases only checked the values posted and properly bound to a model. To be honest, initially, I didnt like that idea. It was confusion to me why my model was attributed with attributes requiring fields, but validation was not happening as I expected.

Over time, I came to appreciate, and apparently exploit this feature to do conditional validation. Since the form only posts the enabled controls, I was able to simply enable/disable inputs and doing so, enable/disable their required attribute for model validation.

Everything worked great, until things changed in RC2.

You can read more about the decision to make the switch.