views:

1381

answers:

3

In running my VS2008 unit integration tests against my DAL, I have found that the assembly is reading machine.config instead of the assembly's app.config.

Here's the rundown on the call stack:

  • Unit Test Project has method calling into a DataLayer Project
  • MyDataLayer class inherits from a base class. Method is called GetStuff()
  • Base class is using System.Configuration . All good.
  • calling this property in code: ConfigurationManager.ConnectionStrings["MyConnStr"] actually returns null because it's not found apparently.
  • checking the MyDataLayer class, yep, MyConnStr is there.
  • checking the collection ConnectionStrings, yes, it has one connection string. It's the one in machine.config that's over in C:\Windows\Microsoft.NET\Framework\v2.0.50727\Config\machine.config
  • I am not sure why my DAL's app.config is being superseded by the machine.config

Any help is appreciated!

+4  A: 

Not sure if this applies to you, but you need to make sure the configuration string is in your Unit Test Project's app.config, not your DataLayer project.

Ray
Ray, you're a monster! All I had to do was reference System.Configuration and copy/share the App.Config from the DataLayer project over to my UnitTest project, and it worked. Bravo!
p.campbell
+1  A: 

This might help to some people dealing with Settings.settings and App.config:

Watch out for GenerateDefaultValueInCode attribute in the Properties pane while editing any of the value (rows) in the Settings.settings grid in Visual Studio (VS2008 in my case).

If you set GenerateDefaultValueInCode to True (True is the default here!), the default value is compiled into the exe (or dll), you can find it embedded in the file when you open it in a plain text editor.

I was working on a console application and if I had defaults in the exe, the application always ignored the config file placed in the same directory!

Quite a nightmare and no information about this on the whole internet.

Roman
A: 

This was driving me nuts - many thanks Roman...

Mike Pelton