0

I am using ASP.NET MVC4, Microsoft.VisualStudio.TestTools.UnitTesting and Jenkins. In my web.config I want to ensure via unit-tests, that the attribute debug is set to false:

<configuration>
[...]
  <system.web>
    <compilation debug="false">
[...]

How can I ensure that, using a unit test?

[TestClass]
    public class WebConfigTest
    {
        /// <summary>
        /// in Web.config, debug mode must be set to 'false'
        /// </summary>
        [TestMethod]
        public void TestDebugFalse()
        {
            // Arrange

            // Act

            // Assert
            Assert.AreEqual(?, "false");
        }
    }
Simon
  • 4,157
  • 2
  • 46
  • 87
  • Why do you want to do this? typically unit test projects have their own config file. – DavidG May 21 '14 at 15:15
  • If you really want to, I assume you can just read the `web.config` file since it's XML and get the node you want. – Jeroen Vannevel May 21 '14 at 15:23
  • Seems like a duplicate of the following: http://stackoverflow.com/questions/6193010/how-to-determine-if-compilation-debug-true-in-web-config – Scott Wylie May 21 '14 at 16:58

1 Answers1

2

Testing configurations like this can give you a false sense of security, because configurations can be changed post-deployment. This is unlike the rest of the code that you're unit testing, which would require a recompilation in order for the changes to take effect.

If you still want to have a test for this situation, though, your test will need to manually load the web.config file and parse it for the expected value. (You can't use the ConfigurationManager, since your test project uses its own app.config instead of the web.config you're targeting.)

For example:

using System.Xml.Linq;
using System.Xml.XPath;

[TestClass]
public class WebConfigTest
{
    /// <summary>
    /// in Web.config, debug mode must be set to 'false'
    /// </summary>
    [TestMethod]
    public void TestDebugFalse()
    {
        var path        = @"Path to web.config";
        var config      = XDocument.Load(path);

        var compilation = config.XPathSelectElement("/configuration/system.web/compilation");
        if(compilation != null)
        {
            var debug = compilation.Attribute("debug");
            if(debug != null)
            {
                Assert.AreEqual("false", debug.Value);
            }
        }
    }
}

Edit: Joe mentioned in the comments that you can alternatively use the WebConfigurationManager to get this information, without the XML parsing. For example:

[TestClass]
public class WebConfigTest
{
    /// <summary>
    /// in Web.config, debug mode must be set to 'false'
    /// </summary>
    [TestMethod]
    public void TestDebugFalse()
    {
        var path                    = @"Path to web.config";
        var webConfig               = new System.IO.FileInfo(path);
        var virtualDirectoryMapping = new System.Web.Configuration.VirtualDirectoryMapping(webConfig.DirectoryName, true, webConfig.Name);
        var fileMap                 = new System.Web.Configuration.WebConfigurationFileMap();

        fileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);

        var configuration           = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(fileMap, "/");
        var compilation             = configuration.GetSection("system.web/compilation") as System.Web.Configuration.CompilationSection;

        if(compilation != null)
        {
            Assert.AreEqual(false, compilation.Debug);
        }
    }
}
Lilshieste
  • 2,744
  • 14
  • 20
  • This should throw an exception if the attribute doesn't exist. – Erik Philips May 21 '14 at 20:10
  • Thanks for catching that. I've updated the example to accommodate null references returned by `XPathSelectElement` and `Attribute`. – Lilshieste May 21 '14 at 20:18
  • Nit-pick: "You can't use the ConfigurationManager" - you can use WebConfigurationManager to do this, as an alternative to parsing XML: http://stackoverflow.com/questions/2368748/how-to-openwebconfiguration-with-physical-path – Joe May 21 '14 at 20:37
  • @Joe Thanks for the info; I was unaware that this existed. I've updated my answer to include the code to use this as an alternative approach. – Lilshieste May 21 '14 at 21:12