Tuesday, April 21, 2015

Starting to use TDD in .NET

I'm currently reading the book Working effectively with Legacy Code, and the main theme is to try to cover all code with unit tests. If the code is difficult to test the main cause is dependencies, and there are a number of ways to deal to break those.

Today I was working on adding a new feature to existing legacy code in C#. I decided to try out the TDD method of doing things. I was successfully able to add unit tests for most of the new code. 

1- changing code that uses RMO to manage replication - i could not figure out an easy way to break dependency, so I tried to use sprout method. That turned out to not work either, so then I was going to refactor the current code so that I could add the sprout method. Then I realized that the end result would be a very trivial method, with the sole purpose of being easier to unit test. So I gave up on this, and did the old way - integration test and then manually checking the results.

2. Changing SQL scripts. These are actually dynamic SQL for creating stored procs, so I added unit tests for the script installer. This was an unforeseen benefit of having to use dynamic SQL - it's covered by tests and since the stores procs are actually created it verifies the syntax and objects. This is an advantage over executing scripts with NOEXEC, or executing them in a transaction. 

3. Parsing data from a SQL query into a data class - the parser method has a SqlDataReader parameter like this

function(SqlDataReader reader)
parse reader

So the method is dependent on that class. The book talks about using fake / mock objects in order to break dependencies. So I changed the parameter to ask for an IDataRecord, which is an interface that SqlDataReader implements. Then I subclassed IDataReader and implemented the methods that my method uses. I called this class FakeSqlDataReader. Then I created the object, populated it with data, and passed it to the parser. Voila, broke that dependency and got the code under test!

No comments:

Post a Comment