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 have the situation where:

1) A test runs mocking a type with the default constructor mocking behavior. ie constructors are mocked.

2) Then a test runs mocking the same type without constructor mocking. But the static initializers are not called when the instance of the mocked type is created.

Is this by design? I would imagine that it should perform static initialization in step 2. As it is it means that if you require static initialization somewhere in your tests you must create every mock of that type with Constructor.StaticNotMocked.
asked by servotest bob (3.8k points)

8 Answers

0 votes
Hi,
This is actually how .NET works.
Only the first time a type is called is the static constructor called.

So if you need to test a type that sometimes is run as normal and sometimes the static constructor needs to be mocked, the easiest way is to put these tests in seperate assemblies.

In any case I will add this to our future features. It would be nice to be able to call the static constructor in your tests.
answered by scott (32k points)
0 votes
:idea: In version 3.5 we now will automatically call static constructors in the case you provided.
answered by scott (32k points)
0 votes
:o I have an NUnit project containing several test assemblies.

My first test assembly implements a test which mocks a Singleton using Natural Mocks:

using {RecordExpectations recordExpectations = RecorderManager.StartRecording())
{
    ....
    NotificationManager.Instance.NotificationArrived += new NotificationArrivedDelegate(Instance_NotificationArrived);
    ....
}


A second test assembly implements tests for the NotificationManager and in my [SetUp]:

this.notificationManager = NotificationManager.Instance;


Please don't flame this but the Singleton implements a double-check and lock pattern:

....
private static volatile NotificationManager instance = null;
private static object lockObject = new object();
....
public static NotificationManager Instance
{
    get
    {
        if (instance == null)
        {
            lock(lockObject)
            {
                if (instance == null)
                {
                    instance = new NotificationManager();
                }
            }
        }
        return instance;
    }
}


:wink: So.... you guessed it.... the first test mocks the static constructors and when I try and call Instance in the second assembly the static is null and the lock fails!

This only occurs when I run the NUnit project as a whole - the second assembly runs fine in isolation (as you would expect).

I have tried calling CallStaticConstructors() in the first test assembly but no joy.

:?: Any ideas?
answered by c0d3-m0nk3y (8.7k points)
0 votes
How do you run both the assemblies.
Do you have two NUnit commands?
answered by scott (32k points)
0 votes
I have an NUnit .nunit project which I load into the Gui - you can add assemblies to the project and run all your unit tests as a suite which is sweet ( :oops: )

You can construct them manually but it is easier to add the assemblies using the NUnit gui and save the .nunit file.

Check out their documentation http://www.nunit.org/index.php?p=projectEditor&r=2.2.
answered by c0d3-m0nk3y (8.7k points)
0 votes
Hi,
I tried this exact scenario and it works as a charm!!

It is surely something else.
Please see if you are referencing public static fields of the Singlton class before calling methods to it (in this case the static constructor will not be called), you can use the Tracer to Visually see when the calls have been made and when they are mocked.

In any case if you cannot solve this, as you belong to our Silver Support, we can solve this offline.
answered by scott (32k points)
0 votes
Thanks - I constructed a sample project of my own and it worked like a charm also so the problem must lay somewhere within the solution.

I will track it down and eliminate it!!! :twisted:
answered by c0d3-m0nk3y (8.7k points)
0 votes
Don't forget that the TypeMock Tracer is there to help
answered by scott (32k points)
...