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,
I have the following test code

     [TestMethod()]
        public void EnumerateTest()
        {
            ResourceEnumerateClientManager target = new ResourceEnumerateClientManager(); 
            string xPathCondition = string.Empty; // TODO: Initialize to an appropriate value

            Message fakeMesage = Isolate.Fake.Instance<Message>();
            Isolate.WhenCalled(() => fakeMesage.IsFault).WillReturn(true);

            Isolate.Fake.StaticMethods<Message>();
            Isolate.WhenCalled(() => Message.CreateMessage(MessageVersion.Default, string.Empty)).WillReturn(fakeMesage);

            EnumerateClient fakeEnumerateClient = Isolate.Fake.Instance<EnumerateClient>();
            Isolate.WhenCalled(() => fakeEnumerateClient.Enumerate(fakeMesage)).WillReturn(fakeMesage);
            Isolate.Swap.NextInstance<EnumerateClient>().With(fakeEnumerateClient);            

            target.Enumerate<ResourceBase>(xPathCondition);

            // Verify expected value

        }


Message is WCF message (System.ServiceModel.Channels.Message). When the following statement is run...

Isolate.WhenCalled(() => Message.CreateMessage(MessageVersion.Default, string.Empty)).WillReturn(fakeMesage);


... I get the following error message

Test method TypeMockTests.UnitTests.EnumerateTest threw exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object..

And the stack is

TypeMock.MockManager.a(String A_0, String A_1, Object A_2, Object A_3, Boolean A_4, Object[] A_5)
TypeMock.InternalMockManager.getReturn(Object that, String typeName, String methodName, Object methodParameters, Boolean isInjected, Object p1, Object p2)
System.ServiceModel.Channels.Message.CreateMessage(MessageVersion version, String action)
b__4() in c:\rakesh\TypeMockTests\TypeMockTests\UnitTests.cs: line 100
System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
System.Delegate.DynamicInvokeImpl(Object[] args)
System.Delegate.DynamicInvoke(Object[] args)
c9.e()
c9.a(Object A_0)
a(T A_0)
TypeMockTests.UnitTests.EnumerateTest() in c:\rakesh\TypeMockTests\TypeMockTests\UnitTests.cs: line 100



Thanks.
asked by (1.6k points)

1 Answer

0 votes
You can solve this issue by moving Isolate.Fake.StaticMethods<Message>(); line to the beginning of your test:
[TestMethod()]
public void EnumerateTest()
{
    Isolate.Fake.StaticMethods<Message>(); // <---

    ResourceEnumerateClientManager target = new ResourceEnumerateClientManager();
    string xPathCondition = string.Empty; // TODO: Initialize to an appropriate value

    Message fakeMesage = Isolate.Fake.Instance<Message>();
    Isolate.WhenCalled(() => fakeMesage.IsFault).WillReturn(true);

    
    Isolate.WhenCalled(() => Message.CreateMessage(MessageVersion.Default, string.Empty)).WillReturn(fakeMesage);

    EnumerateClient fakeEnumerateClient = Isolate.Fake.Instance<EnumerateClient>();
    Isolate.WhenCalled(() => fakeEnumerateClient.Enumerate(fakeMesage)).WillReturn(fakeMesage);
    Isolate.Swap.NextInstance<EnumerateClient>().With(fakeEnumerateClient);           

    target.Enumerate<ResourceBase>(xPathCondition);

    // Verify expected value
}
answered by (11.9k points)
...