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
Hi all,

I have a problem mocking static methods. I use the newest Isolator (6.0.7.0).

The final line in the following code fails with a null reference exception:

[Isolated]
        [TestMethod]
        public void Test()
        {
            XmlDictionaryWriter writer = Isolate.Fake.Instance<XmlDictionaryWriter>();
            Isolate.Fake.StaticMethods<XmlDictionaryWriter>(Members.CallOriginal);
            Stream s = Isolate.Fake.Instance<Stream>();
            Isolate.WhenCalled(() => XmlDictionaryWriter.CreateTextWriter(s)).WillReturn(writer);
        }


Exception:

System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=TypeMock
StackTrace:
at gq.a()
at ee.a()
at h9.a(Type A_0, Object A_1, MethodBase A_2, String A_3, TypeParams A_4)
at h9.a(Object A_0, String A_1, String A_2, MethodBase A_3, Object[] A_4, Object A_5)
at TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Boolean A_5, Object[] A_6)
at TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Boolean isInterceptedType, Object p1)
at System.Xml.XmlDictionaryWriter.CreateTextWriter(Stream stream)
at XXX.FinancialWebservices.Receiver.UnitTest.Chunk.ChunkedMessageReaderTest.<>c__DisplayClass6.<Test>b__5() in
InnerException:


XmlDictionaryReader is from the System.Runtime.Serialization assembly.
asked by kbjt@nnit.com (600 points)

2 Answers

0 votes
Hello,

Thanks very much for the sample code. Unfortunately, this looks like a bug :evil:

Let me poke around some more with this and I'll get back to you with a workaround or (hopefully) a proper solution.
answered by igal (5.7k points)
0 votes
Hello again,

We are working on a fix for this issue, however, as a workaround, you can swap the order of the fake calls, and it makes the test pass:

[Isolated] 
[TestMethod] 
public void Test() 
{ 
    Isolate.Fake.StaticMethods<XmlDictionaryWriter>(Members.CallOriginal);
    XmlDictionaryWriter writer = Isolate.Fake.Instance<XmlDictionaryWriter>(); 
    ...
}


Hope that helps!
answered by igal (5.7k points)
...