Unable to Modify Application Settings when Unit Testing

195 views Asked by At

I am trying to change some Application Settings that live within the Settings of a Windows Service which would be set during installation. The settings I want to set for a Unit Test.

Now, the assemblyinfo file has the InternalsVisibleTo to the Unit Test Project.

In my Test Method I can go drill down to Settings.Default on the Project I want to test and I can see all the settings. However, when I go for instance:

   dummy.PropertyValues["MySetting"].PropertyValue = "SomeValue";

I get a null reference exception.

dummy is set as:

    var dummy = MyProject.Properties.Settings.Default;

Is there away I can set the Application Settings I want for a unit test?

2

There are 2 answers

0
Andy5 On BEST ANSWER

The problem seems to be with Visual Studio 2010.

After restarting it, and as the Unit Test already had access via assembleyInfo.cs, I was able to set the Property Setting as described in my question.

0
vendettamit On

This is something that can get anyone confused. There are two settings available in visual studio projects; one is ProjectSettings and other is Application configuration settings.

Here MyProject.Properties.Settings.Default is your project settings. Application settings are available under System.Configuration.ConfigurationManager.Appsettings. So may be you want to use your dummy object for Application settings:

var dummy = System.Configuration.ConfigurationManager.Appsettings;

Now if you update the value it will be update the Appsettings collection in your current session:

dummy.PropertyValues["MySetting"].PropertyValue = "SomeValue";