chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,
I'm trying to fake a static class and its static members. The first time everything is mocked but the second time the properties are not mocked.

    public static class GlobalLogger
    {
        public static Logger GetClassLogger()
        {
            return new Logger();
        }
    }

   public class Logger
   {
       private SystemLogger m_SystemLogger = new SystemLogger();

       public SystemLogger SystemLogger
       {
            get { return m_SystemLogger; }
       }
   }

    public  class  SystemLogger
    {
        private bool m_IsDebbug = true;

        public  bool IsDebugg
        {
            get { return m_IsDebbug; }   
        }

        public void Write(string  s)
        {
            //I don't want to go there
            throw new Exception();
        }
    }

    public class DummyClass
    {
        private static readonly Logger sLogger = GlobalLogger.GetClassLogger();

        public void DoSomething1()
        {
            if (sLogger.SystemLogger.IsDebugg)
                sLogger.SystemLogger.Write("DoSomething1");
        }

        public void DoSomething2()
        {
            if (sLogger.SystemLogger.IsDebugg)
                sLogger.SystemLogger.Write("DoSomething1");
        }
    }

        [TestMethod]
        [Isolated]
        public void TestMethod1()
        {
            Isolate.Fake.StaticMethods(typeof(GlobalLogger), Members.ReturnRecursiveFakes);
            var d = new DummyClass();
            d.DoSomething1();
        }

        [TestMethod]
        [Isolated]
        public void TestMethod2()
        {
            Isolate.Fake.StaticMethods(typeof(GlobalLogger), Members.ReturnRecursiveFakes);
            var d = new DummyClass();
            d.DoSomething2();
        }



When the second test run I expect that sLogger.SystemLogger != null
but it's not. :(
why sLogger.SystemLogger is != null for the first test and not for the second test?

Thanks
asked by pierre-luc (3.3k points)

6 Answers

0 votes
Hi,

It seems like a bug. SystemLogger should not be null in both tests. I'll try to reproduce it here and confirm it soon.

Regards,
Elisha,
Typemock Support
answered by Elisha (12k points)
0 votes
Hi,

I checked it out and I can confirm it is a bug.
Until we'll fix it try to use the following workaround:
[TestMethod, Isolated]        
public void TestMethod1()
{
    var fake = Isolate.Fake.Instance<SystemLogger>(Members.CallOriginal);
    Isolate.WhenCalled(() => fake.Write(null)).IgnoreCall();
    Isolate.Swap.NextInstance<SystemLogger>().With(fake);
    
    var d = new DummyClass();
    d.DoSomething1();
}


[TestMethod, Isolated]
public void TestMethod2()
{
    var fake = Isolate.Fake.Instance<SystemLogger>(Members.CallOriginal);
    Isolate.WhenCalled(() => fake.Write(null)).IgnoreCall();
    Isolate.Swap.NextInstance<SystemLogger>().With(fake);
    
    var d = new DummyClass();
    d.DoSomething2();
}


I assume from the code you posted that you want to ignore the call to the Write method and that's what the code above will do.

Please let me know if the workaround helps you.
answered by ohad (35.4k points)
0 votes
Actually I cleaned a lot before posting.
In the fact Logger refer to ISystemLogger (interface).

But I found my own workaround, I add if (obj !=null) before using it, even if it's not supposed to be null.

Thank you for the help.
answered by pierre-luc (3.3k points)
0 votes
By the way when I mock in the TestInitialize if I put Isolated in my TestMethod, does it clean what has been mocked in during the TestInitialize?
answered by pierre-luc (3.3k points)
0 votes
Hi,

Glad you found a workaround until we'll fix the bug :)
As for your question: Yes the Isolated attribute will clean up fakes that created in the TestInitialize method but the cleanup code is called after the test method and the TestInitialize method is called before the test method so the init code will work properly.

Hope it helps.
answered by ohad (35.4k points)
0 votes
Hi,
Finally I can't use my workaround.

It was usable for a simple place, but I can't put the workaround every where.

I hope you will be able to fix that bug soon.

Is it possible to mock a static member twice. One time in test1 and another time in test2? With no clean up between each test.
answered by pierre-luc (3.3k points)
...