|
From: Levi K. <lkh...@us...> - 2004-10-13 15:44:37
|
Update of /cvsroot/nmock/nmock/src/NMock/Proxy In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8192/src/NMock/Proxy Modified Files: MockRealProxy.cs Log Message: fixed bug in proxy mock, added unit tests Index: MockRealProxy.cs =================================================================== RCS file: /cvsroot/nmock/nmock/src/NMock/Proxy/MockRealProxy.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** MockRealProxy.cs 24 Aug 2004 21:19:03 -0000 1.2 --- MockRealProxy.cs 13 Oct 2004 15:43:15 -0000 1.3 *************** *** 9,18 **** public class MockRealProxy : RealProxy { ! private readonly IMock mock; private readonly Type type; ! public MockRealProxy(IMock mock, Type type) : base(type) { ! this.mock = mock; this.type = type; } --- 9,18 ---- public class MockRealProxy : RealProxy { ! private readonly IInvocationHandler handler; private readonly Type type; ! public MockRealProxy(Type type, IInvocationHandler handler) : base(type) { ! this.handler = handler; this.type = type; } *************** *** 25,49 **** throw new NotSupportedException("Unsupported call type " + msg.GetType()); } ! if (call.MethodName == "GetType" && call.InArgCount == 0) { return new ReturnMessage(type, null, 0, null, call); } ! return new ReturnMessage(ExecuteCall(call), null, 0, null, call); } ! private object ExecuteCall(IMethodCallMessage call) { string methodName = TypeCheckedMock.StripGetSetPrefix(call.MethodName); ! object result = mock.Invoke(methodName, call.InArgs); if (result == null) { ! MethodInfo method = type.GetMethod(call.MethodName, (Type[])call.MethodSignature); ! if (method.ReturnType.IsValueType) { ! result = Activator.CreateInstance(method.ReturnType); } } return result; } } } --- 25,76 ---- throw new NotSupportedException("Unsupported call type " + msg.GetType()); } ! if (call.MethodName == "GetType" && call.ArgCount == 0) { return new ReturnMessage(type, null, 0, null, call); } ! return ExecuteCall(call); } ! private ReturnMessage ExecuteCall(IMethodCallMessage call) { string methodName = TypeCheckedMock.StripGetSetPrefix(call.MethodName); ! object result; ! try ! { ! result = handler.Invoke(methodName, call.Args); ! } ! catch (Exception e) ! { ! return new ReturnMessage(e, call); ! } ! object returnValue = GetReturnValue(result, call); ! return new ReturnMessage(returnValue, null, 0, null, call); ! } ! ! private object GetReturnValue(object result, IMethodCallMessage call) ! { if (result == null) { ! Type returnType = GetReturnType(call); ! if (returnType.IsValueType) { ! return Activator.CreateInstance(returnType); } } return result; } + + private Type GetReturnType(IMethodCallMessage call) + { + MethodInfo method = type.GetMethod( + call.MethodName, (Type[])call.MethodSignature); + + if (method == null) + { + method = typeof(object).GetMethod( + call.MethodName, (Type[])call.MethodSignature); + } + return method.ReturnType; + } } } |