diff --git a/src/Pose/Pose.csproj b/src/Pose/Pose.csproj index 4638de3..d0c4d50 100644 --- a/src/Pose/Pose.csproj +++ b/src/Pose/Pose.csproj @@ -6,8 +6,8 @@ false - none - + full + diff --git a/src/Pose/PoseContext.cs b/src/Pose/PoseContext.cs index 2375224..fa6d22a 100644 --- a/src/Pose/PoseContext.cs +++ b/src/Pose/PoseContext.cs @@ -59,7 +59,8 @@ public static async Task Isolate(Func entryPoint, params Shim[] shims) #endif // ReSharper disable once PossibleNullReferenceException - await (methodInfo.CreateDelegate(delegateType).DynamicInvoke() as Task); + var task = methodInfo.CreateDelegate(delegateType).DynamicInvoke() as Task; + await task; } } } \ No newline at end of file diff --git a/src/Sandbox/Program.cs b/src/Sandbox/Program.cs index f83889d..fb1556e 100644 --- a/src/Sandbox/Program.cs +++ b/src/Sandbox/Program.cs @@ -24,27 +24,24 @@ public static async Task DoWorkAsync() await Task.Delay(1000); } + private static async Task Run(MyClass myClass) + { + await myClass.DoSomethingAsync(); + } + public static async Task Main(string[] args) { - var staticAsyncShim = Shim.Replace(() => DoWorkAsync()).With( - delegate - { - Console.Write("Don't do work!"); - return Task.CompletedTask; - }); - var taskShim = Shim.Replace(() => Is.A().DoSomethingAsync()) - .With(delegate(MyClass @this) + var myClass = new MyClass(); + var myShim = Shim.Replace(() => myClass.DoSomethingAsync()) + .With( + delegate (MyClass @this) { - Console.WriteLine("Shimming async Task"); + Console.WriteLine("LOL"); return Task.CompletedTask; } ); - await PoseContext.Isolate( - async () => - { - await DoWorkAsync(); - }, staticAsyncShim - ); + + PoseContext.Isolate(() => Run(myClass), myShim); // #if NET48 // Console.WriteLine("4.8"); diff --git a/test/Pose.Tests/ShimTests.cs b/test/Pose.Tests/ShimTests.cs index c92b387..7b485fa 100644 --- a/test/Pose.Tests/ShimTests.cs +++ b/test/Pose.Tests/ShimTests.cs @@ -793,6 +793,62 @@ public void Can_shim_constructor_of_sealed_reference_type() public class AsyncMethods { + public class General + { + private class MyClass + { + public async Task DoSomethingAsync() => await Task.CompletedTask; + } + + [Fact] + public void Can_replace_async_instance_method_for_specific_instance() + { + // Arrange + var myClass = new MyClass(); + var shim = Shim.Replace(() => myClass.DoSomethingAsync()); + + // Act + Action act = () => + { + shim + .With( + delegate (MyClass @this) + { + Console.WriteLine("LOL"); + return Task.CompletedTask; + } + ); + }; + + // Assert + act.Should().NotThrow(because: "the async method can be replaced"); + } + + [Fact] + public void Can_replace_async_instance_method_for_specific_instance_with_async_delegate() + { + // Arrange + var myClass = new MyClass(); + var shim = Shim.Replace(() => myClass.DoSomethingAsync()); + + // Act + Action act = () => + { + shim + .With( + delegate (MyClass @this) + { + Console.WriteLine("LOL"); + return Task.CompletedTask; + } + ); + }; + + // Assert + act.Should().NotThrow(because: "the async method can be replaced"); + } + } + public class StaticTypes { private class Instance