【问题标题】:Configuration settings make tests unstable with NCrunch配置设置使 NCrunch 的测试不稳定
【发布时间】:2016-01-31 16:16:25
【问题描述】:

我遇到了一个问题,即在 NCrunch 下测试变得不稳定。看起来它与一些影子复制问题有关。我的测试是这样的

class SaveViewSettings : ISaveSettings
{
    public void SaveSettings()
    {
        Properties.View.Default.Save();
    }
}

[TestFixture]
// ReSharper disable once InconsistentNaming
class SaveViewSettings_Should
{
    [Test]
    public void Save_Settings()
    {
        var ctx = Properties.View.Default;
        var sut = new SaveViewSettings();

        ctx.LeftSplitter = 12.34;
        sut.SaveSettings();
        ctx.Reload();
        ctx.LeftSplitter.Should().Be(12.34);
    }
}

当使用ctx.Reload() 重新加载设置时,我明白了

System.Configuration.ConfigurationErrorsException : ... 
----> System.Configuration.ConfigurationErrorsException...
(C:\...\AppData\Local\Remco_Software_Ltd\nCrunch.TestRunner.AppDom_Url_q2piuozo0uftcc2pz5zv15hpilzfpoqk\[version]\user.config...)

大约 3 个月前在 NCrunch 论坛上提出了类似的问题:Unrecognized configuration section userSettings

【问题讨论】:

    标签: nunit ncrunch


    【解决方案1】:

    在使用应用程序设置处理多个解决方案时,您可能会在使用 NCrunch 时遇到类似的错误。

    我认为这可能源于 NCrunch 在影子构建时始终使用相同的产品和用户名,以便所有配置设置都映射到相同的 user.config 文件路径。

    似乎到目前为止还没有已知的解决方案。一种解决方法是手动删除用户配置

    %LOCALAPPDATA%\Remco_Software_Ltd\nCrunch.TestRunner.AppDom_...\user.config`.
    

    请注意,执行此操作的常用方法 ctx.Reset() 也可能会失败,因此您必须自己使用 ConfigurationManager 找到并删除 user.config

    我通过添加以下代码来自动解决这个问题,该代码使用 NCrunch 稳定测试

    [Test]
    public void Save_Settings()
    {
    #if NCRUNCH
        // Every once in a while NCrunch throws ConfigurationErrorException, cf.:
        // - http://forum.ncrunch.net/yaf_postsm7538_Unrecognized-configuration-section-userSettings.aspx
        // - http://www.codeproject.com/Articles/30216/Handling-Corrupt-user-config-Settings
        // - http://stackoverflow.com/questions/2903610/visual-studio-reset-user-settings-when-debugging
        // - http://stackoverflow.com/questions/9038070/how-do-i-get-the-location-of-the-user-config-file-in-programmatically
        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
        if (File.Exists(config.FilePath))
            File.Delete(config.FilePath);
    #endif
        ...
    

    在过去几年中,我开始将 .NET 配置设置视为一种遗留功能。它是在 .NET 2.0 中引入的,当时非常棒,但它有一些您需要注意的问题。也许寻找替代方案或抽象是一个好主意,例如HumbleConfig 方便切换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 2016-01-25
      • 2018-07-21
      相关资源
      最近更新 更多