3

I'm trying to run an NUnit test on a method that obtains a connectionstring from the web.config file. When I test it, it obviously crashes when trying to pull that connection string. So I poked around and came to this URL:

Unit testing the app.config file with NUnit

Here is the code the user posted:

void BasicSetup()
{
     ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings();
     connectionStringSettings.Name = "testmasterconnection";
     connectionStringSettings.ConnectionString = "server=localhost;user=some;database=some;port=3306;";
     ConfigurationManager.ConnectionStrings.Clear();
     ConfigurationManager.ConnectionStrings.Add(connectionStringSettings);
}

That almost seemed to be the answer I was looking for. When I actually put in the code, I get the following error: Failure: System.Configuration.ConfigurationErrorsException : The configuration is read only.

Is there something else I must do first? Thanks!

Community
  • 1
  • 1
Jason Thompson
  • 4,643
  • 5
  • 50
  • 74

1 Answers1

0

This link indicates that you need to load up the configuration first. Perhaps that bit of code was excluded from the solution you refer to? The way to do that is something like:

Configuration config = ConfigurationManager.OpenExeConfiguration("test.exe");
abatishchev
  • 98,240
  • 88
  • 296
  • 433
grant
  • 852
  • 1
  • 8
  • 21