0

I'm using Nunit and I wonder if there is a way to return to the real initialization state including all the singletons that constructed already.

currently I've solved it with a workaround of adding to the singletons a reset method and I don't like it because now my production code is not clean.

as far as I understand the single tones will be initialized once and be kept in the heap which is available during all the tests executions.

Is there a way to clean the heap at the teardown function of the Nunit testfixture?

I've managed to reproduce the issue with the following (ugly) code sample, you can see that when both tests executed one after the other the second one failed...

[TestFixture]
public class SingletonCleanups
{
    private MySingleTonsHolder _holder;
    [SetUp]
    public void Setup()
    {
        _holder = new MySingleTonsHolder();
    }
    [Test]
    public void DoWork_FirstExecution_SingleCalledOnce()
    {
        _holder.DoWork();
        Assert.AreEqual(1, MySingleTonsHolder.MySingleTon.CalledCount);
    }
    [Test]
    public void DoWork_SecondExecution_SingleCalledOnce()
    {
        _holder.DoWork();
        Assert.AreEqual(1, MySingleTonsHolder.MySingleTon.CalledCount);
    }
}

public class MySingleTonsHolder
{
    public static MySingleTon MySingleTon => MySingleTon.Instance();

    public void DoWork()
    {
        MySingleTon.Instance().CalledCount++;
    }
}

public class MySingleTon
{
    private static MySingleTon _instance;
    public static MySingleTon Instance()
    {
        if (_instance==null)
        {
            _instance = new MySingleTon();
        }
        return _instance;
    }
    public int CalledCount { get; set; }
    private MySingleTon()
    {
        CalledCount = 0;
    }
}
silver
  • 1,633
  • 1
  • 20
  • 32
  • is it possible to provide a sample test class which reproduces the issue? its hard to tell what you are trying to do exactly – nozzleman Nov 29 '16 at 08:07
  • Is it possible to restart your program under test or reinitialize the whole component? A discussion about the problems which occur using singletons in unit tests can be found here: http://stackoverflow.com/questions/3876951/why-is-it-hard-to-unit-test-a-system-that-depends-on-singletons – JoeFox Nov 29 '16 at 08:18

1 Answers1

-1

The thing here that unit tests shows to you very useful stuff: static fields in code is EVIL. now my production code is not clean=> It wasnt clean, because of statics. If you want to introduce singletone, do this please using DI framework, which you use, or without DI framework, but with IoC.

For more information, pls go to anwser from here Dependency Injection & Singleton Design pattern

Community
  • 1
  • 1
tym32167
  • 4,741
  • 2
  • 28
  • 32
  • you're right static fields and singletons are EVIL, having said that I think that the DI frameworks won't solve this issue because the singleton object will be kept between the tests... – silver Nov 29 '16 at 10:34
  • @silver Yes, if you are keeping something in static fields, DI wont help to you. – tym32167 Nov 29 '16 at 10:40
  • so it's not answering my question :( – silver Nov 29 '16 at 11:33