Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 511
Draco Ater Avatar asked Nov 05 '10 11:11

Draco Ater


2 Answers

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.

like image 64
Kipp Avatar answered Sep 19 '22 20:09

Kipp


You just add your "App.Config" file to the unit test project . It will read automatically.

like image 36
Shanthi Gopalan Avatar answered Sep 18 '22 20:09

Shanthi Gopalan