Search code examples
c#unit-testingumbraco

Mock Umbraco configuration in test project


When running unit tests on code using some Umbraco Core extensions, such as string.ToUrlSegement(), the following error is thrown:

System.Configuration.ConfigurationErrorsException: Could not load the Umbraco.Core.Configuration.UmbracoSettings.IUmbracoSettingsSection from config file, ensure the web.config and umbracoSettings.config files are formatted correctly

How do you prevent this happening?


Solution

  • Beside copying all Umbraco settings into your test projects app.config, you can add the following configuration:

    <configSections>
        <sectionGroup name="umbracoConfiguration">
            <section name="settings" type="Umbraco.Core.Configuration.UmbracoSettings.UmbracoSettingsSection, Umbraco.Core" requirePermission="false" />
        </sectionGroup>
    </configSections>
    

    This prevents that exception being thrown when using Umbraco core functions dependent on configuration of a main project.

    Discovered this and some other great tips here.