How To Read UnitTest Project's App.Config From Test With HostType("Moles")

Draco Ater picture Draco Ater · Nov 5, 2010 · Viewed 14k times · Source

I have the folowing tests:

[TestClass]
public class GeneralTest
{
    [TestMethod]
    public void VerifyAppDomainHasConfigurationSettings()
    {
        string value = ConfigurationManager.AppSettings["TestValue"];
        Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
    }

    [TestMethod]
    [HostType("Moles")]
    public void VerifyAppDomainHasConfigurationSettingsMoles()
    {
        string value = ConfigurationManager.AppSettings["TestValue"];
        Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
    }
}

The only difference between them is [HostType("Moles")]. But the first passes and the second fails. How can I read App.config from the second test?

Or may be I can add some another config file in other place?

Answer

Kipp picture Kipp · Dec 31, 2011

Assuming you are trying to access values in appSettings, how about just adding the configuration at the beginning of your test. Something like:

ConfigurationManager.AppSettings["Key"] = "Value";

Then when your test tries to read the AppSettings "Key", "Value" will be returned.