|
using System;
using System.IO;
using System.Reflection;
using Haack.Example;
using NUnit.Framework;
namespace UnitTests.Haack.Configuration
{
/// <summary>
/// Performs a quick test showing that the settings object
/// does indeed track changes to the config file.
/// </summary>
[TestFixture]
public class ConfigurationTests
{
/// <summary>
/// Grabs an instance of MyStuff, overwrites the config file, then
/// makes sure the instance has its settings updated.
/// </summary>
[Test]
public void TestTrackChanges()
{
//Grab the settings object.
// Could have used the static MyStuff.Settings property as well.
MyStuff settings = (MyStuff)System.Configuration.ConfigurationSettings.GetConfig("MyStuff");
//Assert the original values
Assertion.AssertEquals("A bunch of information", settings.Bar);
Assertion.AssertEquals(1.234, settings.Foo);
//Ok, we're ready to make a change to the config file.
WriteConfigFile("UnitTests.Haack.Configuration.NewApp.config");
//Let's give a small bit of time for the change to propagate.
System.Threading.Thread.Sleep(200); //plenty of time.
//Moment of truth. Did the settings object get updated
//with the new values??? The public wants to know!
//Note the same instance of settings.
Assertion.AssertEquals("Nothing useful here.", settings.Bar);
Assertion.AssertEquals(2.718281, settings.Foo);
}
/// <summary>
/// Writes the embedded App.config file into the appropriate directory.
/// </summary>
[SetUp]
private void SetUp()
{
WriteConfigFile("UnitTests.Haack.Configuration.App.config");
}
// Overwrites the config file with the file embedded in the unit test assembly.
void WriteConfigFile(string resourceName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
using(Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
{
using(StreamReader reader = new StreamReader(resourceStream))
{
using(StreamWriter writer = new StreamWriter(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile))
{
writer.Write(reader.ReadToEnd());
writer.Flush();
}
}
}
}
/// <summary>
/// Deletes the config file.
/// </summary>
[TearDown]
private void TearDown()
{
try
{
File.Delete(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
}
catch(Exception e)
{
Console.WriteLine("Something bad happened." + e.Message);
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.