0

I have a problem with tests becoming unstable under NCrunch. It looks like it has to do with some shadow copying issue. My test goes something like this

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);
    }
}

When reloading the settings using ctx.Reload() i get

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

A similar problem has been raised on the NCrunch forum about 3 months ago: Unrecognized configuration section userSettings

mkoertgen
  • 898
  • 6
  • 14

1 Answers1

0

You might get similar errors with NCrunch when working on multiple solutions with application settings.

I think this might root down to NCrunch always using the same product and user name when shadow building so that all configuration settings are mapped to the same user.config file path.

It seems that by now there is no known solution to this. A work around is to manually delete the user config in

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

Note that the usual way to do this ctx.Reset() might fail as well, so you really have to locate and delete the user.config yourself using ConfigurationManager.

I have automated this work around by adding the following code which stabilizes the test with 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
    ...

Over the past few years i came to consider .NET configuration settings as a kind of legacy feature. It was introduced with .NET 2.0 and was great at the time but it has some issues that you need to be aware of. Maybe it is a good idea to look for alternatives or abstractions like e.g. HumbleConfig which makes it easy to switch.

mkoertgen
  • 898
  • 6
  • 14