In testing singleton classes we need the single instance to "go away" after each test. Is there a way to configure nunit to recreate the test app domain after each test, or at least after each fixture?
A:
I guess I'm missing something here Ralph. Just for my own benefit, can you explain why adding methods with the following attributes to your Test Class that drop and recreate your instances wouldn't work for you?
Adding these attributes for methods should make them run before / after each test.
[SetUp]
[TearDown]
Adding these attributes for methods should make them run before / after the fixture.
[TestFixtureSetUp]
[TestFixtureTearDown]
Is there a reason why using methods with these attributes couldn't create and destroy your domain between tests?
Paddyslacker
2010-03-15 21:07:20
+1
A:
You could provide the means to renew the singleton instance when testing via a conditional method.
// CUT
public sealed class Singleton{
private static Singleton _instance = new Singleton();
private Singleton()
{
// construct.
}
public static Singleton Instance{
get{
return _instance;
}
}
[Conditional ("DEBUG")]
public static void FreshInstance (){
_instance = new Singleton();
}
}
// NUnit
[TestFixture]
public class SingletonTests{
[SetUp]
public void SetUp(){
Singleton.FreshInstance();
}
}
panamack
2010-03-20 22:00:04