public bool TestGenerics <T>(T repository, LoggedCall call) where T : class { var t = repository.GetType(); var targetType = typeof(T); var recorder = Mimic.Record(repository); var method = targetType.GetMethod(call.MethodName, call.Parameters.Select(p => Type.GetType(p.ParameterExpectedType)).ToArray()); try { method.Invoke(recorder, call.Parameters.Select(arg => arg.Value).ToArray()); } catch (TargetException e) { throw new ObjectIsNotOfExpectedTypeException(t, targetType); } catch (TargetInvocationException e) { // this is already logged by proxy recorder } var callReturnValue = call.ReturnValue; var recordedCall = Mimic.GetHistory(recorder).Calls[0]; var isEqual = (recordedCall.ThrownExceptionType == null && String.IsNullOrWhiteSpace(call.ThrownExceptionType)) && comparer.Equals(recordedCall.ReturnValue, callReturnValue) || (!String.IsNullOrWhiteSpace(call.ThrownExceptionType) && recordedCall.ThrownExceptionType == call.ThrownExceptionType); VerificationLog.Add(new BehaviourLogItem() { Pass = isEqual, Call = call, ActualCall = recordedCall }); return(isEqual); }
private Parameter[] GetParameters(IInvocation invocation) { var copier = Mimic.GetCopier(); var output = new List <Parameter>(); var methodParams = invocation.Method.GetParameters(); foreach (var param in methodParams) { var type = invocation.Arguments[param.Position] != null ? invocation.Arguments[param.Position].GetType() : param.ParameterType; output.Add( new Parameter() { Name = param.Name, ParameterActualType = type.AssemblyQualifiedName, ParameterExpectedType = param.ParameterType.AssemblyQualifiedName, Value = copier.Copy(invocation.Arguments[param.Position]) }); } return(output.ToArray()); }
private LoggedCall ConvertToLoggedCall(IInvocation invocation) { Type t; if (invocation.ReturnValue != null) { t = invocation.ReturnValue.GetType(); } else { t = invocation.Method.ReturnType; } var copier = Mimic.GetCopier(); return(new LoggedCall() { MethodName = invocation.Method.Name, MethodDeclaringTypeName = invocation.Method.DeclaringType.AssemblyQualifiedName, ReturnValue = copier.Copy(invocation.ReturnValue), ReturnType = t.AssemblyQualifiedName, Parameters = GetParameters(invocation) }); }
public static void SaveHistoryToFile(object proxyObject, string filePath) { var serialised = Mimic.SerialiseHistory(proxyObject); File.WriteAllText(filePath, serialised); }