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:

[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.

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.

Wed, Apr 17, 2013

Up & Running with TypeScript and WebStorm

I love my iMac and I’m on a mission to find a language I enjoy that I can use my Mac for (no Windows fan boy jokes please). There’s something in my mind I associate with work and my Windows laptop. Therefore, I don’t feel to excited about getting my laptop out of my bag in the evenings/weekends to play with other stuff.

As I want to broaden my knowledge I wanted to find something ideally statically typed (although I’m currently looking into Python) that would work on OS X. I’ve said previously that JavaScript seems the way to go in my current situation so I thought I’d take a look at TypeScript and also use WebStorm from Jetbrains as my IDE seeing as I’ve heard so many great things about them and their products (don’t worry I use ReSharper).

TypeScript

So I went over to TypeScript’s website and followed the Hello World type code examples under the “Learn” tab.

Menu