Unit Testing with SqlException

So after a nice Christmas break I get to some code that needs some unit testing around a try/catch. Something similar to this:

try
{
    myService.DoSomethingThatMightTakeALongTime();
}
catch (EntityCommandExecutionException ex)
{
    var exception = ex.InnerException as SqlException;
    if (exception != null)
    {
        if (exception.Number == -2)
        {
            //Do something special
        }
    }
}

Read more...

Using SQL Server with node.js

I like to keep eyes and ears open for new technologies and methodologies in order to become a better developer and I’d heard about edge.js many months ago but made a mental note of it and waved it goodbye. edge.js lets you have two-way communication between node and C# libraries. When I first looked at it I thought that sounded a bit hacky, I’ve spent my time communicating with COM libraries in Delphi and OCX libraries with C# and didn’t like it so I felt this was pretty much the same thing. A long time passed and I was writing a console based Windows app as a service and had wondererd whether I could quickly port it to node.

I was discussing with a colleague about using node at work and that we needed something seperate and small just to try it out and see how the whole developement process with it worked. As the database that this app needed to communicate with was MSSQL I looked into a library on NPM that would communicate with MSSQL and maybe act as an ORM. There was a Microsoft lib that seemed untouched and reading the comments on the issues list on Github it didnt favour too well. There were libraries that would communicate with MySQL & PostgresSQL but not MSSQL. In my search I came across edge.js again. It had 2 samples, one that used edge-sql and one that used ScriptCS so in laymans terms, one that used a precompiled dll and one that used a C# script that was executed at runtime.

Read more...

Octopus XML Transformation in Services

We use Octopus Deploy at work and its a superb tool for deploying your applications whether they be websites or *.exes.

One of the great things it also provides is the ability to use Microsoft’s Transformation process for config files. However, when deploying a exe application its a bit trickier than a website. Unfortunately the documentation doesn’t mention the steps needed to get this working so read on!

Typically a web application will have web.config and a web.Release.config as well as other derivations you may use. Octopus also supports web.[Environment].config.

Read more...

Using Git to update YouTrack via TeamCity

This post is mainly a reminder for me as I keep forgetting the command in Git to integrate commits to YouTrack items. YouTrack uses TeamCity to get the information about the commits and then scans the commit comment for a YouTrack item id and any commands that it can apply such as item status or time spent on said item. There is some documentation here but its not the greatest in terms of clarity and I’ve spoken to Hadi Hariri from JetBrains about improving this so hopefully they’re working on it.

Read more...

Running Mocha tests within Sublime Text

I spend most of my day in Visual Studio with lots of the goodies an IDE can offer. One of them being able to run your tests from a keystroke.

In a bid to expand my mind I’m working on a little project that is made up of JS entirely so I’ve dug out Sublime Text. It has lots of plugins that are very handy, especially Sublime-HTMLPrettify which will tidy your HTML, CSS & JS for you.

When writing tests for JS there are many libraries you can use but I’ve chosen Mocha for now. The one thing I couldn’t work out was to run my tests within Sublime Text until now.

Build System

Sublime allows you to have build systems a bit like an IDE so you can tell it what to do when you invoke it via cmd+B.

To get Mocha to run we need to create a new build system. To do this click Tools - Build System - New Build System and paste in the below:

Read more...

Blogging with Markdown & Deploying via Git - Introducing Sandra.Snow

There are many markdown blogging engines out there such as Calepin, Scriptogram and even WordPress allows you to write blog posts in Markdown but Sandra.Snow tries to add something different. Firstly, it is written in .Net and Nancy, secondly its a static blog generator and finally it supports Git deployment.

Even if you don’t want to use Git deployment you can use FTP, its a great tool. To write your blog post in Markdown you need a custom header in your file so it knows some information about your post.

---
layout: post
category: Azure
title: Setting up a ServiceStack Service
---

It then parses this information along with your Markdown into its engine, uses a Markdown view engine to convert the file content into HTML, assign model properties based on the header and creates a HTML file using the model via a Razor viewengine.

The “layout” refers to the Razor file it uses to render the final HTML file. This allows you to style your pages and blog posts whichever way you’d prefer. These “layout” files should exist in the “_layouts” folder for your site template. The site template is a set of files and folders that Sandra.Snow uses to produce the final static website.

The “category” or “categories” property, you can use both for singular or multiple comma-seperated values that refer to the category/categories of your blog post.

The “title” should hopefully be self explanatory!

You can optionally add an author and email properties to override the global config settings for example, if you wanted to allow guest author blog posts. There is also an optional metadescription property you can use for SEO.

Read more...

Returning multiple fake objects with FakeItEasy

I was recently writing some unit tests where I needed to test that multiple calls to an interface returned different objects.

With FakeItEasy this is easy:

A.CallTo(() => myInterface.GetSomething(1)).Returns(new Something())

All very nice, but now if I have multiple calls to myInterface I have to execute the above statement ‘x’ amount of times:

[Fact]
public void Should_Do_Something()
{
  var myInterface = A.Fake<IApplication>();
  A.CallTo(() => myInterface.GetSomething(1)).Returns(new Something());
  A.CallTo(() => myInterface.GetSomething(2)).Returns(new Something());
  A.CallTo(() => myInterface.GetSomething(3)).Returns(new Something());

  var result = sut.DoSomething(myInterface);

  Assert.Equal("Super Duper", result);
}

Read more...

Enabling CORS in IISExpress

I was playing around with swagger-ui and was trying to point it to a local endpoint that I started with IIS Express. I was getting an error saying that it needed the endpoint to accept Access-Control-Allow-Origin requests. I went Googling and it couldn’t find anything specific to IIS Express but managed to use some guidance for full blown IIS. The solution is to go to C:\Program Files (x86)\IIS Express\AppServer and open the applicationhost.

Read more...

Keeping SQL Data Organised in Integration Tests

In my latest project I had kept my solution tidy with my main app project, my unit test project and integration test project. I tend to stick with a naming convention such as MainApp, MainApp.Tests.Unit & MainApp.Tests.Integration.

I had begun writing my integration tests for a repository that hits the database and returns data. Currently it was one method being called in the repository. xUnit allows you to setup any test dependencies in the constructor of your test class. It also allows you to do any tidying up in a Dispose method if you implement IDisposable although this is frowned upon. However I felt for my needs I would implement this.

I was creating data in the database in the constructor which will get called before the test runs, retrieving data in the test, asserting and then deleting all data and resetting the auto-incrementing from the tables in the Dispose method.

This was working perfectly until I wanted to test another method on my repository.

I now needed to add data for my new method but realised if I added different data to the database in the constructor, I would be creating unnecessary data unrelated to the test.

My options were to move the constructor logic into separate methods and then call the methods in the test or have separate test classes per method in the repo. Both were a not an ideal solution and quite frankly verbose, ugly and not best practice.

Read more...

Comparing object instances with FakeItEasy

I had the task of writing a new application recently and of course I chose Nancy. One of the many great reasons is the testing capabilites it offers (For more on that see this great series of articles).

The basics of a test with Nancy looks like this:

[Fact]
public void Should_return_status_ok_when_route_exists()
{
    // Given
    var bootstrapper = new DefaultNancyBootstrapper();
    var browser = new Browser(bootstrapper);

    // When
    var result = browser.Get("/", with => {
        with.HttpRequest();
    });

    // Then
    Assert.Equal(HttpStatusCode.OK, result.StatusCode);
}

You set up a bootstrapper, this can be your live one or an inherited version of your live one with dependencies changed to mocks for example or use the ConfigurableBootstrapper.

Read more...