Search code examples
c#configurationtdddecoupling

TDD with Web Config


All great stories they always start with those 4 magical words... I have inherited a system... no wait! that isn't right!

Anyway with my attempt at humour now passed I have not so much been given more I have to support an existing service.

There are many many issues when it comes to using this service, as an example one is to create a record of a person, you need to call 4 different parts of the services.

So, getting together with my Manager we decided that we need to stick another layer on top to add a facade to the common requests, to simplify the number things and the correct order to do them when creating a new site.

My question starts here if anyone wants to avoid the above waffle

So I want to use TDD on the work I am doing, but the service I have inherited (which will become our Data layer) has been strongly coupled with a database connection string located in a specific connetionstring node in the Web.Config.

Problem I have is, to decouple the service from the Config file will take weeks of my time which I do not have.

So I have had to add and App.Config file with the expected node into my Test project.

Is it ok to do this, or should I start investing some time to decouple the database config from the datalayer?


Solution

  • I agree that you should probably look into using Dependency Injection as your working your way through the code to decouple your app from the config, however, I also understand that doing that is not going to be an easy task.

    So, to answer your question directly, no, there is nothing wrong with adding a config file to support your tests. This is actually quite common for unit testing legacy systems (legacy being an un-tested system). I have also, when left with no other option, resorted to utilizing reflection to "inject" fake configuration values into the ConfigurationManager in order to test code that is reading configuration values, but this is probably a last resort.