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 mocking MS enterprise library classes. Here is the sample test

 [TestFixture, Isolated]
    public class Tests
    {

        [Test,Isolated]
        public void UninitializedFieldExceptionDemo()
        {
            Database m_Database = Isolate.Fake.Instance<Database>();
            Isolate.WhenCalled(() => DatabaseFactory.CreateDatabase()).WillReturn(m_Database);
            DbConnection m_Connection = Isolate.Fake.Instance<DbConnection>();
            Isolate.WhenCalled(() => m_Database.CreateConnection()).WillReturn(m_Connection);
            SomeClass m_ClassMock = Isolate.Fake.Instance<SomeClass>();
        }
    }

    public class SomeClass
    {
        
    }



execution of it fails with exception:
TypeMock.TypeMockException:
*** The call to System.Text.RegularExpressions.ExclusiveReference.Get() was made on an uninitialized field.
at System.Text.RegularExpressions.ExclusiveReference.Get()
at System.Text.RegularExpressions.Regex.Run(Boolean quick, Int32 prevlen, String input, Int32 beginning, Int32 length, Int32 startat)
at System.Text.RegularExpressions.Regex.IsMatch(String input)
at dy.Instance<T>(Members behavior)
at dy.Instance<T>()
at Tests.Tests.UninitializedFieldExceptionDemo() in Tests.cs: line 19
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected)
at Tests.Tests.UninitializedFieldExceptionDemo() in Tests.cs: line 0



Tests.cs line 19 is:
 SomeClass m_ClassMock = Isolate.Fake.Instance<SomeClass>();



I can provide a demo solution if it may help.
asked by knst (2.9k points)

3 Answers

0 votes
I've tried reproducing this exception but the test passed...
I'll need more details to investigate this issue - let's take it offline
answered by dhelper (11.9k points)
0 votes
I just started mocking some of our MS EL stuff today (but we're already on 5.2.2) - I was having a problem with the Caching appllication block.

I worked around mine by in the test class initialize making a 'call' to the CacheFactory.GetCacheManager, then in my tests I mock as normal. This seems to 'kick' any static constructors into the right place. If I don't do this I get config errors after my green tests (isolated tests) run and then my couple of integration tests execute.

I suspect 1 or 2 things in my case:

There are static constructors that when using Isolator mocks first end up causing those static constructors to not have issues in memory that it's expecting, or the clean up code for AAA isn't cleaning up static fields that were set in static constructors. My experience leads me to believe it's EL, but you never know.

HTH.
answered by boo (21.8k points)
0 votes
Brian,

In version 5.2.2 we had an issue faking Enterprise library that caused the exception specified in the beginning of this thread.

I'll send you a patch and see the issue you've encountering is resolved as well
answered by dhelper (11.9k points)
...