We are having problems accessing information in .net configuration files (such as app.config and web.config) via unit tests when the unit tests have a host type of "Moles". It's causing quite a few headaches, so I hope someone has an idea on what can be done.
We're using Visual Studio 2010, and I believe we've tried this on a machine with VS 2010 SP1 installed, and machine without SP1 installed, as well as trying it on 32 bit and 64 bit machines.
I've taken the liberty of reducing the test to its simplest terms. The problem can be recreated by composing a unit testing project consisting of the following two files, and running the test after uncommenting the only commented line. The test works without a host type, but when you introduce Moles as a host type, the null assertion in the test fails. We are not sure why.
First, the configuration file App.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="Connection" connectionString="Something" />
</connectionStrings>
</configuration>
Next, the test class containing a single test:
namespace TestProject
{
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class UnitTest
{
[TestMethod]
//[HostType("Moles")]
public void TestMethod()
{
var data = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Assert.IsNotNull(data.ConnectionStrings.ConnectionStrings["Connection"]);
}
}
}
I would be grateful if anyone could offer any insight.
Thanks very much,
Nick
I'm not sure if it will do the job, but you can try this workaround: open the config using a file mapping. The code will look like this: