无法读取 HostType(“Moles”)上的 ConfigurationManager.AppSettings

发布于 2024-12-12 20:06:16 字数 314 浏览 2 评论 0原文

如果 HostType 为 Moles,那么我无法通过 ConfigurationManager.AppSettings 获取数据,

它在 2-3 个月前对我来说工作正常。但它不起作用,我的所有测试用例都失败了(引发了空引用异常)。

请帮我修复。谢谢。

MSDN 中还提出了相关主题< /a>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽月 2024-12-19 20:06:16

这是 .Net 4.0 中当前版本的 Moles 中的一个已知错误 - 您可以使用 Mole 配置管理器并让它返回您想要的值作为解决方法。

作为示例,这就是我如何为 Linq To SQL DataContext 设置测试,以在不连接到 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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文