private object CreateMock(Type returnType, MocksRepository repository, Invocation invocation) { var parentMock = invocation.MockMixin; var replicator = parentMock as IMockReplicator; object mock = null; if (returnType.IsArray) { mock = Array.CreateInstance(returnType.GetElementType(), Enumerable.Repeat(0, returnType.GetArrayRank()).ToArray()); } var idictionaryType = returnType.GetImplementationOfGenericInterface(typeof(IDictionary <,>)); if (mock == null && idictionaryType != null) { var dictType = typeof(Dictionary <,>).MakeGenericType(idictionaryType.GetGenericArguments()); mock = MockCollection.Create(returnType, repository, replicator, (IEnumerable)MockingUtil.CreateInstance(dictType)); } var ienumerableType = returnType.GetImplementationOfGenericInterface(typeof(IEnumerable <>)); if (mock == null && ienumerableType != null) { var listType = typeof(List <>).MakeGenericType(ienumerableType.GetGenericArguments()); mock = MockCollection.Create(returnType, repository, replicator, (IEnumerable)MockingUtil.CreateInstance(listType)); } if (mock == null && typeof(Task).IsAssignableFrom(returnType)) { var elementType = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>) ? returnType.GetGenericArguments()[0] : typeof(object); var taskResultValue = MustReturnMock(invocation) ? CreateMock(elementType, repository, invocation) : elementType.GetDefaultValue(); Expression <Func <Task <object> > > taskFromResult = () => MockingUtil.TaskFromResult((object)null); mock = ((MethodCallExpression)taskFromResult.Body).Method .GetGenericMethodDefinition() .MakeGenericMethod(elementType) .Invoke(null, new object[] { taskResultValue }); } if (mock == null && MustReturnMock(invocation, checkPropertyOnTestFixture: true)) { if (typeof(String) == returnType) { mock = String.Empty; } else { try { mock = replicator.CreateSimilarMock(repository, returnType, null, true, null); } catch (MockException) { } } } return(mock); }
private object CreateMock(Type returnType, MocksRepository repository, Invocation invocation) { var parentMock = invocation.MockMixin; var replicator = parentMock as IMockReplicator; object mock = null; if (returnType.IsArray) { mock = Array.CreateInstance(returnType.GetElementType(), Enumerable.Repeat(0, returnType.GetArrayRank()).ToArray()); } var idictionaryType = returnType.GetImplementationOfGenericInterface(typeof(IDictionary <,>)); if (mock == null && idictionaryType != null) { var dictType = typeof(Dictionary <,>).MakeGenericType(idictionaryType.GetGenericArguments()); mock = MockCollection.Create(returnType, repository, replicator, (IEnumerable)MockingUtil.CreateInstance(dictType)); } var ienumerableType = returnType.GetImplementationOfGenericInterface(typeof(IEnumerable <>)); if (mock == null && ienumerableType != null) { var listType = typeof(List <>).MakeGenericType(ienumerableType.GetGenericArguments()); mock = MockCollection.Create(returnType, repository, replicator, (IEnumerable)MockingUtil.CreateInstance(listType)); } if (mock == null && typeof(Task).IsAssignableFrom(returnType)) { var elementType = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>) ? returnType.GetGenericArguments()[0] : typeof(object); var taskResultValue = MustReturnMock(invocation) ? CreateMock(elementType, repository, invocation) : elementType.GetDefaultValue(); Expression <Func <Task <object> > > taskFromResult = () => MockingUtil.TaskFromResult((object)null); mock = ((MethodCallExpression)taskFromResult.Body).Method .GetGenericMethodDefinition() .MakeGenericMethod(elementType) .Invoke(null, new object[] { taskResultValue }); } #if !PORTABLE if (mock == null && returnType.IsByRef) { var delegateType = typeof(object).Assembly.GetType("Telerik.JustMock.RefDelegate`1").MakeGenericType(new [] { returnType.GetElementType() }); ConstructorInfo constructor = delegateType.GetConstructor(new[] { typeof(object), typeof(IntPtr) }); MethodInfo genericMethodInfo = this.GetType().GetMethod("GetDefaultRef", BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo methodInfo = genericMethodInfo.MakeGenericMethod(returnType.GetElementType()); mock = constructor.Invoke(new object[] { this, methodInfo.MethodHandle.GetFunctionPointer() }); } #endif if (mock == null && MustReturnMock(invocation, checkPropertyOnTestFixture: true)) { if (typeof(String) == returnType) { mock = String.Empty; } else { try { mock = replicator.CreateSimilarMock(repository, returnType, null, true, null); } catch (MockException) { } } } return(mock); }
public static IAssertable TaskResult <T>(this IFunc <Task <T> > expectation, T result) { return(ProfilerInterceptor.GuardInternal(() => expectation.Returns(MockingUtil.TaskFromResult(result)))); }