public void Process(Invocation invocation) { if (invocation.Recording || invocation.InArrange || invocation.InAssertSet) { return; } var returnType = invocation.Method.GetReturnType(); if (!typeof(Task).IsAssignableFrom(returnType)) { MockingContext.Fail("Wrong invocation to arrangement: return type of {0}.{1} is not a task", invocation.Instance != null ? MockingUtil.GetUnproxiedType(invocation.Instance) : invocation.Method.DeclaringType, invocation.Method.Name); } var elementType = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(Task <>) ? returnType.GetGenericArguments()[0] : typeof(object); Expression <Func <Task <object> > > taskFromException = () => MockingUtil.TaskFromException <object>((Exception)null); var mock = ((MethodCallExpression)taskFromException.Body).Method .GetGenericMethodDefinition() .MakeGenericMethod(elementType) .Invoke(null, new object[] { this.exception }); var parentMock = invocation.MockMixin; var mockMixin = MocksRepository.GetMockMixin(mock, null); if (parentMock != null && mockMixin != null) { parentMock.DependentMocks.Add(mock); } invocation.ReturnValue = mock; invocation.CallOriginal = false; invocation.UserProvidedImplementation = true; }