Presenting ConfigurationFileSession

Here is a little class that allows you to use different configuration files. I find it extremely useful for tests where i want to mock the values that would be retrieved via the ConfigurationManager. Here are a couple of examples how it can be used:

[TestMethod]
public void ShouldUseSystemTimeWhenNoValuesAreProvided()
{
 using (new ConfigurationFileSession("WithoutDateTimeManipulation.config"))
 {
  ApplicationEnvironment.Instance.Refresh();

  DateTime now = DateTime.UtcNow;
  TimeSpan allowedDifference = TimeSpan.FromSeconds(1);
  TimeSpan actualDifference = ApplicationEnvironment.Instance.CurrentDateTime - configurationNow;
  Assert.IsTrue(actualDifference  < allowedDifference);
 }
}

[TestMethod]
public void ShouldUseValuesAsProvided()
{
 using (new ConfigurationFileSession("WithDateTimeManipulation.config"))
 {
  ApplicationEnvironment.Instance.Refresh();

  DateTime now = new DateTime(2000, 1, 1);
  TimeSpan allowedDifference = TimeSpan.FromSeconds(1);
  TimeSpan actualDifference = ApplicationEnvironment.Instance.CurrentDateTime - configurationNow; 
  Assert.IsTrue(actualDifference < allowedDifference);
 }
}

Feel free to download the code from BeTimvwFramework.

This entry was posted on Monday, September 22nd, 2008 at 17:08 and is filed under C#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply