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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Under NUnit 2.2.7, .NET 1.1...

I have a class that has a logging service that gets initialized during its static construction phase and used during the instance constructor, something like this:

public class Foo{
  private static LogService logSvc = LogServiceFactory.GetLogService();

  public Foo(){
    logSvc.Debug("Class constructor running.");
  }
}


In some tests I want to make sure that I can get to the class without running the static constructor so I don't get all hung up on the log service. So I create a mock:

Foo obj = (Foo)RecorderManager.CreateMockedObject(typeof(Foo));


That works really well... until other fixtures run that actually need to create the object:

Foo obj = new Foo();


When this happens, I get a null reference exception in the object's constructor because the static constructor never gets run.

Thinking out loud, I suppose one possible solution would be to not mock the static constructor and instead mock the LogServiceFactory return value... wouldn't it? But then wouldn't that mean the mock return value has to stick around for later? Hmmm... maybe that wouldn't work.

Anyway, this is a real difficult thing to debug - when you're running a body of several hundred (or several thousand) unit tests, many of which use mocking, and seeing tests for the Foo object fail... then when you ONLY run the Foo object tests, they succeed. It's tough to know where to even start on something like that.

I was lucky and it turned out that I found the few unit tests mocking the Foo static constructor and was able to successfully allow the static constructor to run unmocked, solving the issue. I'd have been hosed if I couldn't do that. It might be something to consider addressing for the future, though I don't know if I have any suggestions - what if you have tests run that need to mock the static constructor... then some that want it to have run... then some more that need it to have been mocked? That doesn't work, either.

This may also be a unit test framework shortcoming - if every test ran in its own AppDomain, wouldn't the whole issue become moot?
asked by tillig (6.7k points)

8 Answers

0 votes
Travis,

:idea: This feature is already implemented in TypeMock.NET version 3.5

When a static constructor is mocked, it won't run as long as you are running mocked code, but once you are running normal (non-mocked) code, TypeMock will call the static constructor.
This will work even when you want to mock it in another test, because the static constructor is called only once and won't be called anyway.

But you have found a bug :twisted: This doesn't work when using the new [VerifyMocks] decorator.
Is this your problem? We will fix it and send you a patch.
answered by scott (32k points)
0 votes
:idea: I sent a fix off line.
This bug happens when ClearAll()/VerfiyAll() is called more then once.
It will be incorporated in our next patch
answered by scott (32k points)
0 votes
I did forget to post my TypeMock version, didn't I? I'm on 3.6.0.0.

I will try out the fix you sent and let you know what happens. Thanks!
answered by tillig (6.7k points)
0 votes
My bad. Looking at the wrong assembly. I meant 3.7.1.0.
answered by tillig (6.7k points)
0 votes
Unfortunately things seem to not be cooperating with me just upgrading the assembly. I think I'm having some fusion errors due to assembly version numbers and such:

System.MissingMethodException: Method not found: Boolean TypeMock.InternalMockManager.isMocked(System.Object, System.String, System.String, System.Object).

Unfortunately, I'm not in a position where I can upgrade my TypeMock installation to 4.0 quite yet so I'll have to take the fix on faith. :)

When I get a chance to actually test it out, I'll let you know how it goes.
answered by tillig (6.7k points)
0 votes
Travis,
The patch is for TypeMock 4.0
I hope that you will be able to upgrade soon.
Do you have any idea when?
answered by scott (32k points)
0 votes
I've started the ball rolling and hopefully it should be within the next few business days. I have high hopes for the upgrade by mid-next-week. I will work my hardest to get this in quickly, and do apologize for the delay.
answered by tillig (6.7k points)
0 votes
I received a patch from support and it properly fixes the issue. Should be released in 4.0.2 (shortly). Thanks!
answered by tillig (6.7k points)
...