Posts Tagged ‘development’

Isolating Your Configuration Files For Testability

Tuesday, July 27th, 2010


Ben Hall over at CodeBetter just posted a nice article on using the Castle Dictionary Adapter to isolate your code from the ConfigurationManager class to make your unit tests easier to write.

I thought I might take the opportunity to talk about a technique that we use to isolate our code from ConfigurationManager and also other system classes such as the system DateTime, using the language constructs built within the .Net Framework version 3.5 and above. I’m not saying that this technique is better than the Castle Dictionary Adapter method but it just highlights an alternative approach that is possible without using third party assemblies.

Ok so using Ben’s example, because I’m too lazy to think up my own, we have an application configuration value for our EnableNewsLetterSignup.

<add key="EnableNewsletterSignup" value="false"/>

In our code we pull out the value using the static method on the ConfigurationManager class like so.

public bool Signup(string email)


    if (!Boolean.Parse(ConfigurationManager.AppSettings["EnableNewsletterSignup"]))

        return false;


    return true; // technically this would go off to an external web service


Ok so nothing new here but when it comes to testing this code it means that we have to have a configuration file for our unit test project (which is bad) and we can only ever test one path through the code without changing the value of the configuration file. Which means we some untestable code without doing some configuration Kung Fu on our config file which is more work than we should have to do.

Wrapping It Up

The technique we use is to wrap our class up in a static wrapper class which we will call Config. For our corresponding application setting we add a public static field of the same name for clarity, of type Func<string>. Now the important thing is we give it it’s default behaviour which in this case is to go off and use our old friend ConfigurationManager to determine whether or not our newsletter is enabled or not.

public static class Config


    public static Func<string> EnableNewsletterSignup = () => ConfigurationManager.AppSettings["EnableNewsletterSignup"];



Writing Our Code

Now going back to our code where we use the application setting it will now look something like this.

public bool Signup(string email)


    if (!Boolean.Parse(Config.EnableNewsletterSignup()))

        return false;


    return true; // technically this would go off to an external web service



Testing Our Code

So now when we want to unit test this piece of code we simply change the default behaviour of our Config class to the behaviour we want to test. See line 5 below, where we force it to return false for the purpose of our unit test. We now have total control over what our supposedly configuration value is without ever having to touch the file system.

   1: [Test]

   2: public void If_our_newsletterSignup_is_not_enabled_we_should_not_be_signed_up()

   3: {

   4:     // Arrange

   5:     Congig.EnableNewsletterSignup = () => "false";


   7:     // Act

   8:     var isSignedUp = _classUnderTest.Signup("");


  10:     // Assert

  11:     Assert.IsFalse(isSignedUp);

  12: }


Other Uses

As I said earlier this technique can also be quite useful for isolating other system dependant classes such as code that uses the DateTime.Now property. Sometimes if you want to logic concerning dates and times it can be pretty hard to control the flow of the code because the DateTime is generated by the system. How for example do you test something if you need to be some date in the future or some date in the past. The code below can be used in the same way to isolate our tests from the underlying call to the DateTime.Now which can be overriden as required to give the behaviour we want.

public static class SystemDateTime


   public static Func<DateTime> Now = () => DateTime.Now;



This approach offers an alternative solution to using the Castle Dictionary Adapter,  and although it requires writing slightly more code it does mean that it is one less assembly that you have to reference in your project which can be a good thing sometimes.

CodeBurner Firebug extension

Wednesday, June 23rd, 2010

I just found this nice add in for Firebug called CodeBurner. Essentially it adds in a lot of HTML & CSS reference documentation. The nice thing about it is that when your browsing your HTML or CSS within Firebug you can right click an element and either get a description and get an example of its usage see the screenshot below.




Or you can get information within which browsers that particular element is supported.




Quite useful…

Mocking HtmlHelpers in ASP.Net MVC

Friday, December 4th, 2009

NB: This post relates to version 1 of ASP.Net MVC

I recently added a static extension method for generating a custom drop down list and wanted to test it using Rhino Mocks (version 3.5).
One issue I kept banging my head against was how to create a Mock HtmlHelper in order to call the method correctly.
A number of posts later, and various attempts at using some of the sample code and I finally came across this post detailing a bunch of MVC tests.

After a minor amount of customising, I ended up with the following, which works like a charm for me to use in my tests.

        /// Creates a HTML helper for use in tests.
        private HtmlHelper CreateHtmlHelper()
            var userControl = new ViewUserControl();
            var container = new ViewPage();
            var context = MockRepository.GenerateMock<ViewContext>();
            userControl.ViewContext = context;

            return userControl.Html;

Hands off my mouse

Thursday, May 14th, 2009

Roger Johansson at 456 Berea Street posted this article today which reminded me how simple it can be to test your site for certain aspects of accessibility, in this case keyboard navigation of the site.

Unplug your mouse!

Can you still use your site effectively? If not, it is not up to scratch :)
Seriously simple genius. I love that.

Why SCRUM will never be the same to me again

Wednesday, April 1st, 2009

In my first foray into SCRUM we have had a great deal of success in ensuring widespread understanding of development issues and progress. This has led to a great sense of cohesive development, with everyone aware of and looking out for other issues and I would wholeheartedly recommend it to anyone.
While outlining this to a colleague at work I came up with the following summary of my experience with SCRUM.

  • Catchup – gives everyone a chance to understand what happened yesterday and intentions for today’s development, keeps everyone informed.
  • Understanding – sharing intentions and discussing risks and issues found during development, at a very high level, has helped the team approach things in a great way with a real sense of developing as a team.
  • No Timelines – this is a SCRUM meeting, not a project management meeting. Estimates, and deadlines are not discussed in the confines of the SCRUM meeting. If anyone is having issues, it is raised offline with the project manager where appropriate.

Of course, within seconds, my colleague had noted the acronym that comes out of that lot. And that is why SCRUM meetings will never be the same for me again.

Anyone who works with me should need no more than one guess as to whom the insightful developer is :)