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'm running Isolator 5.1.2.0 on Windows Server 2008 Enterprise, using VS 2008. My tests have this class initializer:

        [ClassInitialize]
        public static void ClassInitialize(TestContext context)
        {
            // This method can only be called once.
            Isolate.Fake.StaticConstructor(typeof(SPUtility));
        }


I do a full rebuild of my solution. When I run the tests under VS (menu item Test, Run, All Tests in Solution), all but one of the tests fail with the error:

Class Initialization method [...].ClassInitialize threw exception. TypeMock.TypeMockException: TypeMock.TypeMockException:
*** Static constructor for class SPUtility cannot be faked as it has already been called.

But when I run the tests from the command line -- or, more precisely, when I run MSBuild and build my projects "UnitTest" target, which runs this command:

$(TypeMockPath)TMockRunner.exe -logpath $(TypeMockLogPath) MSTest.exe /testcontainer:$(inetroot)privateTestUnitTestin$(Configuration)MyProject.UnitTest.dll

... then the tests pass. This is repeatable.

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

2 Answers

0 votes
Hi Larry

Can it be that another test class is using Isolate.Fake.StaticConstructor(typeof(SPUtility))?
The new version that is will be out in few days will handle this more gracefully i.e will not throw an exception when this happens.

I'm sending you a patch.
Please tell if it helps.
answered by ohad (35.4k points)
0 votes
Thanks ohad. Yes, the problem was that I had more than one test class whose [ClassInitialize] method called Isolator.Fake.StaticConstructor on the same type. My workaround is the class below:


using System;
using System.Collections.ObjectModel;
using TypeMock.ArrangeActAssert;

internal static class FakeStaticConstructor
{
    private static readonly Collection<Type> initializedTypesCollection = new Collection<Type>();

    internal static void For(Type type)
    {
        lock (initializedTypesCollection)
        {
            if (!initializedTypesCollection.Contains(type))
            {
                Isolate.Fake.StaticConstructor(type);
                initializedTypesCollection.Add(type);
            }
        }
    }
}


My [ClassInitialize] methods now call FakeStaticConstructor.For(Type) rather than calling Isolator.Fake.StaticConstructor directly:

[TestClass]
public class MyTestClass
{
    [ClassInitialize]
    public static void ClassInitialize()
    {
        FakeStaticConstructor.For(typeof(SPUtility));
    }
}


Thanks for the help!
Larry
answered by lgolding@microsoft.c (4k points)
...