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
I am unit testing SharePoint code that calls methods of the SPUtility class. My tests fail because the SPUtility class's static ctor throws a null reference exception, even though I have mocked the static ctor. Here is a minimal repro, using a static class of my own instead of SPUtility (just in case there was something odd about the SPUtility class):

namespace TypeMockTest
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TypeMock.ArrangeActAssert;

    public static class MyStaticClass
    {
        static MyStaticClass()
        {
            throw new Exception("MyStaticClass cctor should not be called.");
        }

        public static string Foo()
        {
            return "Foo";
        }
    }

    [TestClass]
    public class StaticConstructorFailure
    {
        [TestMethod, Isolated]
        public void StaticConstructorFails()
        {
            Isolate.Fake.StaticConstructor(typeof(MyStaticClass));
            Isolate.Fake.StaticMethods(typeof(MyStaticClass));

            Isolate.WhenCalled(() => MyStaticClass.Foo()).WillReturn("Bar");
        }
    }
}


This test fails because the "Isolate.WhenCalled" line throws the exception from the static ctor, even though the static ctor is mocked.

Thanks,
Larry
asked by lgolding@microsoft.c (4k points)

5 Answers

0 votes
Hi.
I am able to reproduce this. as soon as I can give you a good answer as to a workaround or fix for this I will post on this forum and send you an email

Thanks,
Roy
answered by royo (2k points)
0 votes
Thanks royo!
answered by lgolding@microsoft.c (4k points)
0 votes
Hi,

We found out the root cause for this problem: this occured due to the static constructor being called during the WhenCalled() recording instead where it was expected. This is now fixed on a patch and will be released on the next Isolator version. A patch is going out by email to lgolding.

Doron
Typemock Support
answered by doron (17.2k points)
0 votes
Thanks again, royo and doron. I'm really impressed to get a patch within three days after reporting the bug. That was fast!
answered by lgolding@microsoft.c (4k points)
0 votes
This issue was fixed in Isolator 5.2.0
answered by dhelper (11.9k points)
...