Not able to read ConfigurationManager.AppSettings on HostType("Moles")

702 Views Asked by At

If HostType as Moles, then i can't get data via ConfigurationManager.AppSettings

Its work fine for me 2-3 months before. But it is not worked and all my test cases are failed (raised the null reference exception).

Please help me to fix. thanks.

Related thread raised in MSDN also

1

There are 1 best solutions below

1
On

This is a known bug in current versions of Moles in .Net 4.0 - You can Mole the configuration manager and have it return the values that you want as a workaround.

As an example, This is how I setup a test for a Linq To SQL DataContext to get code coverage for a default constructor without connecting to SQL.

[TestInitialize]
[HostType("Moles")]
public void Setup()
{
    MCodesConfigurationHelper.DataAccessSettingsGet = () =>
    {
        MCodesDataAccessSettings setting = new MCodesDataAccessSettings();
        setting.DatabaseConnectionStringGet = () => { return "this is a connection string."; };
        return setting;
    };

    MConfigurationManager.ConnectionStringsGet = () =>
    {
        MConnectionStringSettingsCollection strings = new MConnectionStringSettingsCollection();
        strings.ItemGetString = (string connectionString) =>
        {
            var cstring = new MConnectionStringSettings();
            cstring.ToString = () => { return connectionString; };
            cstring.ConnectionStringGet = () => { return connectionString; };
            return cstring;
        };
        return strings;
    };
    MDataContext.ConstructorString = (DataContext dataContext, string connection) => { };
}


[TestMethod]
[HostType("Moles")]
public void DefaultConstructorTest()
{
    using (MyDataContext target = new MyDataContext())
    {
        Assert.IsNotNull(target);
    }
}