Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing a method that uses configurationmanager in a class library used by a web app

I'm attempting to write a test for the code behind a web service. This code lives in a separate class library called from the service itself. This class library uses ConfigurationManager to get its connection strings, normally when you run the web service those are populated by the web service's web.config. However, when I run it from my test code they throw null exceptions.

I've trolled around the net for answers for a while now and attempted several fixes for roughly similar problems, but nothing worked. (Nothing was exactly my situation.)

like image 582
Eric J Fisher Avatar asked Oct 07 '22 10:10

Eric J Fisher


1 Answers

It depends on the version of Visual Studio that you have, but if you have VS 2010 you can use Moles to mock static classes and in VS 2012 you have Microsoft Fakes.

Nevertheless, as Joachim Isaksson said in his comment, if you are actually doing a Unit Test, you could wrap access ConfigurationManager to the through an interface and a wrapper, and just mock the interface.

like image 71
Damian Schenkelman Avatar answered Oct 10 '22 03:10

Damian Schenkelman