views:

23

answers:

1

I am really starting to enjoy unit testing and have the following question to the gurus of unit testing.

Let's for example say I have the following class

public class FileMapper
{
   public Dictionary<string, string> ReadFile(string filename, string delimeter){}
}

How do you guys generally go about unit testing a Parser or ReadFile method in my case?

+4  A: 

Given the method signature you provided, you can 'simply' unit test the ReadFile method by invoking it with a lot of different input and verify that the return value is correct.

However, this can lead to Obscure Tests because very important test input is hidden away in files instead of being visible when you review each test.

This is where TDD shows its force because it should prompt us to consider a better API.

You could, for example, change the method to this:

public Dictionary<string, string> ReadFile(TextReader reader, string delimeter)

It's still pretty easy to get a TextReader from a file, but now you can alternatively use a StringReader to supply unit test input.

This change will not only make it easier to unit test the ReadFile method, but also make the method more generally usable because it no longer has a tight coupling to the file system.

Mark Seemann
Thank you so much for your insight,I am always trying to steer clear of having to need configuration in my unit tests as I think unit-testing should be configureless.Now, the task of just picking varying inputs for the test.
PieterG