Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

22 Sep 2008

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.