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.

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>