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

We use log4net in various classes for logging, where our loggers are statically initialized like so:

public class MyClass 
    private static ILog _log = LogManager.GetLogger(typeof(MyClass));

We noticed that when using Isolator to mock certain classes and method calls, all the ILog instances are substituted with mock implementations, even though non of our test code specifies as such when using Isolator. As a result, many of our tests fail because Isolator is throwing exceptions stating "Unexpected call to ILog.IsDebugEnabled", etc.

It is even more of a problem because we use 3rd party libraries like NHibernate that also leverage log4net, and it's causing exceptions there too because the ILog instances there have been substituted with mocks.

Is Isolator automatically mocking private static fields of classes? Is there any way to turn this off, or perhaps instruct Isolator to call the original log4net method implementation?
asked by JJS (600 points)

1 Answer

0 votes

First you should check if all the tests that are using the Isolator are decorated with the Isolated attribute. You can put the attribute on the class level and it will automatically will apply to all the test methods.
The Isolated attribute will clean fakes of types that stays 'alive' in the process.

Also when you create a fake by calling Isolate.Fake.Instance<SomeClass>()
the default is recursive fakes which means all the methods including constructors will be faked and all the objects that are returned from the class methods are faked. This can cause uninitialized fields in the class.
You can change it by Isolate.Fake.Instance<SomeClass>(Members.CallOriginal)
This will change the default behavior to call the original implementation of the class.

Hope it helps.
answered by ohad (35.4k points)