Thu, Sep 12, 2013

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.

Wed, Sep 11, 2013

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:

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 => {
    // 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.

Sat, Aug 24, 2013

Async Route Handling with Nancy

I don’t know about you but all I hear is “ASYNC ALL THE THINGS!”, I think this is partly down to its new and shiny and us developers love “the shiny” and partly a lot of the things we do in our applications are I/O based whether that be database or file system.

The problem that comes with the new and shiny bandwagon is you need to understand what you’re evangelising. Making asynchronous methods and executing them with no actual reason will not give your codebase any gains and could actually effect your application’s performance. There is more depth to that argument but for simplicity just remember this, only use asynchronous methods if you are doing some sort of I/O.

It could also be argued that only “use asynchronicity in a web framework if you expect high traffic in your web application”. If you only have 10 requests on a small site you’re not going to benefit from asynchronous execution as there are plenty of threads available to handle your application. If you start hitting 1000 concurrent requests (the default IIS limit) then requests will start getting queued up. If you make your routes asynchronous then any code that is being waited on, the thread that is being used there can be released to process another request thus speeding up the performance of your app and prevent the likely hood of large queues. I will show how simple it is to make your routes asynchronous with Nancy below.

Thu, Aug 22, 2013

A quick look at Visual Node

I came across Visual Node a few months ago and was excited by the looks of it. For those that didn’t click that link, it basically brings the power of Visual Studio debugging to a node.js app. You can write your node.js app in Visual Studio, fire up the debugger by hitting F5 and use breakpoints and watches to see what’s going on.

The hipster in me is screaming saying “You should be using Sublime Text and node-inspector for debugging” but to be honest I found it a bit hackety-hack and it seemed a bit odd debugging my server app in Chrome but maybe that’s just something I need to get over. JavaScript is getting a huge surge in popularity recently so its your duty as a developer to investigate this. I want to learn and understand JS better but I always get frustrated with it after 10mins and swear that I’m never going to touch a dynamic language again, “give me a statically typed language every time with a compiler”. I have a bit of a Jekyll and Hyde situation going on that I need to overcome.

Wed, Jul 3, 2013

Modifying files within Git history

If you have been doing code changes and committing as you go and then look back at the changes you may see something you don’t like the look of. Assuming no-one has a copy of your code changes you can go back and modify the files at a certain point in time within your commit history.

I use Git Bash by default but the editor sucks compared to Sublime Text so first things first lets setup the Git editor.

For Sublime Text run this in the command line:

git config --global core.editor "'C:/Program Files/Sublime Text 2/sublime_text.exe'"

Next up is finding the commit id you want to go back to, to edit:

git log

Make a note of the parent commit id of the commit you want to edit.

Wed, May 29, 2013

Why .Net should become independent!

I recently changed jobs and as usual was at the mercy of recruitment agents. The advert for my job contained things like ASP.Net MVC, Entity Framework & TFS (luckily there were other cool pieces of technology on that list and what the role entailed interested me and once I had joined the company I saw they were open to other tech/approaches that made people’s workflow and output more beneficial to developers as well as the company. In fact I implemented an API written in Nancy on my first day and paved the way for Git in the first week).

My point being that whenever I hear from recruiters or look for jobs all the adverts basically list the full Microsoft stack. I recently heard from a friend who runs his own company that he gave his CV to a recruitment agent and was basically rejected because his .Net experience was not MS based enough. I know his .Net skills are very good but because those .Net skills were put to good use using OSS projects he is unlikely to get a job in the mainstream .Net market.

These adverts usually contain a list of tech/experience similar to: MVC, Webforms, Visual Studio, SQL Server, Entity Framework, WCF, LINQ.

Q: What’s the common denominator here?
A: They are all owned by Microsoft.

Q: What operating system do these all run on?
A: Microsoft Windows

Q: What framework and programming language do they run on?
A: Microsoft .Net and C#

Spot a pattern?

So lets point out the obvious, the operating system, the frameworks, the language, the tooling and the data storage are all owned and implemented by one company (and they say Apple tries to lock its users in).

Tue, Apr 30, 2013

Mocking HttpContext with Fake It Easy

Lets start with the conclusion first and say “use Nancy for your web applications and APIs” as its brilliant!

If you want to continue reading lets crack on.

I’m currently working on a ASP.Net MVC project and one of the controller methods writes directly to the Response, eg. Response.Write(“How will I mock thee?”);

Having moved over to xUnit and FakeItEasy recently I wanted to write a unit or integration test depending how you see it to assert against the Http Response.

Doing this is no easy feat with MVC (with Nancy its all done for you) and you have to mock a lot of things. I’m hoping that in later releases this will be fixed because I know that ASP.Net Web API has made things a bit easier for testing (and wrote a testing library for it) so I assume the two projects will use bits of each other or their roadmap will merge.