14 May 2012

The Extract and Override pattern

I'm in the process of reading Roy Osherove's book, "The Art of Unit Testing". It's a great book - recommended for both Gurus and N00bs; I've learnt a couple of new things.

One of those ideas is the "Extract and Override" pattern. It is a dependency breaking technique described in "The Art of Unit Testing", but I think the name stems from Michael Feather's book, "Working Effectively with Legacy Code".

I'll describe the scenario where this pattern can help. Imagine you need to unit test a class, BookingManager. You specifically want to test the MakeBooking method. Note how the MakeBooking method depends on other methods and those methods use external dependencies:

What the Extract and Override pattern allows is for you to test the MakeBooking method in isolation, without the all the logic and external dependencies in the other methods it depends on. You extract a sub class from the class under test, and *override* the methods you don't care about. Those method can return predefined values. Like so:

We had to mark the methods we are overriding as virtual. The test can create an instance of THIS class instead of the actual class, but it will still test the actual MakeBooking method. The methods our MakeBooking method depends on are stub methods - they simply succeed.

This pattern helps you to isolate the method you want to test from other methods in the same class under test. Pretty useful!
14 May 2012

The Extract and Override pattern

I'm in the process of reading Roy Osherove's book, "The Art of Unit Testing". It's a great book - recommended for both Gurus and N00bs; I've learnt a couple of new things.

One of those ideas is the "Extract and Override" pattern. It is a dependency breaking technique described in "The Art of Unit Testing", but I think the name stems from Michael Feather's book, "Working Effectively with Legacy Code".

I'll describe the scenario where this pattern can help. Imagine you need to unit test a class, BookingManager. You specifically want to test the MakeBooking method. Note how the MakeBooking method depends on other methods and those methods use external dependencies:

What the Extract and Override pattern allows is for you to test the MakeBooking method in isolation, without the all the logic and external dependencies in the other methods it depends on. You extract a sub class from the class under test, and *override* the methods you don't care about. Those method can return predefined values. Like so:

We had to mark the methods we are overriding as virtual. The test can create an instance of THIS class instead of the actual class, but it will still test the actual MakeBooking method. The methods our MakeBooking method depends on are stub methods - they simply succeed.

This pattern helps you to isolate the method you want to test from other methods in the same class under test. Pretty useful!

4 May 2012

Published Interface

Martin Fowler defines a Published Interface as a class that is used outside the code base it is defined in. It's more public than public. I think we need a keyword for this in C# (veryPublic? :). 

4 May 2012

Handling error: "401 - Unauthorized: Access is denied due to invalid credentials"

I struggled with an error from IIS after copying a website from one server to another: "401 - Unauthorized: Access is denied due to invalid credentials"

The website app pool identity was set to ApplicationPoolIdentity, and that user had full rights to the physical folder path. So I had no idea why I would be getting permissions errors.

Finally my colleague, Richard, helped me figure out that assigning rights to the AppPool user was not enough. You also have to give folder rights to the local IUSR account. Note that this is different from the IIS_IUSRS account.

Lovely.

15 Mar 2012

Long running integration tests

I'm typing this blog post while waiting for our integration tests to finish executing. For this project the tests take about 15 minutes to complete.  There are 178 integration tests.

I love the fact that the integration tests run several times a day. But I hate how long it takes to get my work integrated into the main branch (Edge/Trunk) of our repository.

If integration tests fail once or twice, it means integrating can take as much as two hours.

I wonder how other people feel about running integration tests as part of the build. Is it a good idea? Or a bad one?

I think it's definitely great for quality. It's just a pain in the ass.
9 Mar 2012

Thoughts about software projects

Software projects are risky. So plan to deliver a week or two earlier than the deadline. Yes, I know it feels like you will "waste" that week or two and that you won't be 1-billion percent productive. Trust me - you won't be wasting that time. There's always loads more to do. Unexpected things always happen once you release to the "Production" server.

Software projects are risky. Shit happens. So play it safe. Release early. Then do lots of UAT.
7 Mar 2012

Code Snippet: Method to convert Variable sized list to a fixed size array

I wrote this bit of code that takes a List<String> (variable size obviously) and converts it to an array of specified size as requested by the caller.

 Useful. And fun to write. Especially since I had loads of Unit Tests checking that I didn't break anything while refactoring quite aggressively.
16 Feb 2012

Reflection: I'm a versatile person

We can be so hard on ourselves, so it's nice to take a step back and think about what we do well.

I'm going to take advantage of my own blog and do a little bragging today. In relation to my job, I am actually a very versatile person. 
  • I code nice front-ends.
  • I code advanced back-ends.
  • I'm good at Debugging weird issues.

But I'm more than just a code monkey. I also do the following things pretty well:
  • I'm organized - I get things done.
  • I write well - proposals and blog posts.
  • I do presentations.
  • I design.
  • I'm professional and friendly when I deal with clients.
  • I even do network admin stuff.
That's a lot to be proud of I think.

There are some things that I'm not so good at though, but practice makes perfect:
  • Coordinating a project
  • Dealing with project politics
  • Fighting with managers and clients (i.e. pushing back against unrealistic expectations)
  • Handling too much pressure
14 Feb 2012

Different kinds of viruses/virii

A colleague, Justus, described the different kinds of viruses/virii to me. Interesting stuff...

  • A Trojan - opens a "back door" into your system. Just like the story about the Trojan Horse.
  • A Worm - propagates aggressively across your system and the network.
  • A Rootkit - the most advanced. Attaches itself onto the operating system kernel and hides itself. Very difficult to detect and very dangerous.
Nasty little fuckers.
1 Feb 2012

An interesting difference between the ASP.NET Dev Server and IIS

By default IIS will not return static content like CSS and JS files through the ASP.NET pipeline. So Global.asax won't be invoked.

But on the ASP.NET Dev server all files, including CSS and JS files, will go through the ASP.NET pipeline. So Global.asax will be invoked.

Remember this when you're profiling your application! It can lead to skewed results.

willem odendaal's Space

I'm a programmer living in Cape Town South Africa. I love tight security and slick UX. I work for @newMediaLabs