From ac958b90f9daea860d71cce789af3532bcfcc095 Mon Sep 17 00:00:00 2001 From: Ricky Kaare Engelharth Date: Wed, 2 Oct 2024 08:10:58 +0200 Subject: [PATCH] Make WaitForCall extension accept matching calls made before waiting --- src/Atc.Test/SubstituteExtensions.cs | 207 ++++++++++++++++++--------- 1 file changed, 137 insertions(+), 70 deletions(-) diff --git a/src/Atc.Test/SubstituteExtensions.cs b/src/Atc.Test/SubstituteExtensions.cs index 593680c..83dddd7 100644 --- a/src/Atc.Test/SubstituteExtensions.cs +++ b/src/Atc.Test/SubstituteExtensions.cs @@ -1,4 +1,6 @@ // ReSharper disable AsyncVoidLambda +using NSubstitute.Exceptions; + namespace Atc.Test; /// @@ -83,20 +85,29 @@ public static async Task WaitForCall( TimeSpan timeout = default) where T : class { - var completion = new TaskCompletionSource(); - substitute - .When(substituteCall) - .Do(_ => completion.TrySetResult(true)); + try + { + substitute + .ValidateCallReceived( + substituteCall, + MatchArgs.AsSpecifiedInCall); + } + catch (ReceivedCallsException) + { + var completion = new TaskCompletionSource(); + substitute + .When(substituteCall) + .Do(_ => completion.TrySetResult(true)); - await completion - .WaitForCompletion(timeout) - .ConfigureAwait(false); + await completion + .WaitForCompletion(timeout) + .ConfigureAwait(false); - substitute - .ValidateCallReceived( - substituteCall - ?? throw new ArgumentNullException(nameof(substituteCall)), - MatchArgs.AsSpecifiedInCall); + substitute + .ValidateCallReceived( + substituteCall, + MatchArgs.AsSpecifiedInCall); + } } /// @@ -114,19 +125,29 @@ public static async Task WaitForCall( TimeSpan timeout = default) where T : class { - var completion = new TaskCompletionSource(); - substitute - .When(substituteCall) - .Do(_ => completion.TrySetResult(true)); + try + { + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.AsSpecifiedInCall); + } + catch (ReceivedCallsException) + { + var completion = new TaskCompletionSource(); + substitute + .When(substituteCall) + .Do(_ => completion.TrySetResult(true)); - await completion - .WaitForCompletion(timeout) - .ConfigureAwait(false); + await completion + .WaitForCompletion(timeout) + .ConfigureAwait(false); - substitute - .ValidateCallReceived( - x => substituteCall(x), - MatchArgs.AsSpecifiedInCall); + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.AsSpecifiedInCall); + } } /// @@ -139,26 +160,40 @@ await completion /// The call to wait for. /// Timeout for the wait operation. /// A task representing the async operation. + [SuppressMessage( + "Reliability", + "CA2012:Use ValueTasks correctly", + Justification = "Call should not be awaited for route to work")] public static async Task WaitForCall( this TSubstitute substitute, Func> substituteCall, TimeSpan timeout = default) where TSubstitute : class { - var completion = new TaskCompletionSource(); - substitute - .When(substituteCall) - .Do(_ => completion.TrySetResult(true)); + try + { + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.AsSpecifiedInCall); + } + catch (ReceivedCallsException) + { + var completion = new TaskCompletionSource(); + substitute + .When(substituteCall) + .Do(_ => completion.TrySetResult(true)); - await completion - .WaitForCompletion(timeout) - .ConfigureAwait(false); + await completion + .WaitForCompletion(timeout) + .ConfigureAwait(false); - substitute - .ValidateCallReceived( - async x => await substituteCall(x) - .ConfigureAwait(false), - MatchArgs.AsSpecifiedInCall); + substitute + .ValidateCallReceived( + x => substituteCall(x) + .ConfigureAwait(false), + MatchArgs.AsSpecifiedInCall); + } } /// @@ -176,20 +211,29 @@ public static async Task WaitForCallForAnyArgs( TimeSpan timeout = default) where T : class { - var completion = new TaskCompletionSource(); - substitute - .WhenForAnyArgs(substituteCall) - .Do(_ => completion.TrySetResult(true)); + try + { + substitute + .ValidateCallReceived( + substituteCall, + MatchArgs.Any); + } + catch (ReceivedCallsException) + { + var completion = new TaskCompletionSource(); + substitute + .WhenForAnyArgs(substituteCall) + .Do(_ => completion.TrySetResult(true)); - await completion - .WaitForCompletion(timeout) - .ConfigureAwait(false); + await completion + .WaitForCompletion(timeout) + .ConfigureAwait(false); - substitute - .ValidateCallReceived( - substituteCall - ?? throw new ArgumentNullException(nameof(substituteCall)), - MatchArgs.Any); + substitute + .ValidateCallReceived( + substituteCall, + MatchArgs.Any); + } } /// @@ -207,19 +251,29 @@ public static async Task WaitForCallForAnyArgs( TimeSpan timeout = default) where T : class { - var completion = new TaskCompletionSource(); - substitute - .WhenForAnyArgs(substituteCall) - .Do(_ => completion.TrySetResult(true)); + try + { + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.Any); + } + catch (ReceivedCallsException) + { + var completion = new TaskCompletionSource(); + substitute + .WhenForAnyArgs(substituteCall) + .Do(_ => completion.TrySetResult(true)); - await completion - .WaitForCompletion(timeout) - .ConfigureAwait(false); + await completion + .WaitForCompletion(timeout) + .ConfigureAwait(false); - substitute - .ValidateCallReceived( - x => substituteCall(x), - MatchArgs.Any); + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.Any); + } } /// @@ -232,26 +286,39 @@ await completion /// The call to wait for. /// Timeout for the wait operation. /// A task representing the async operation. + [SuppressMessage( + "Reliability", + "CA2012:Use ValueTasks correctly", + Justification = "Call should not be awaited for route to work")] public static async Task WaitForCallForAnyArgs( this TSubstitute substitute, Func> substituteCall, TimeSpan timeout = default) where TSubstitute : class { - var completion = new TaskCompletionSource(); - substitute - .WhenForAnyArgs(substituteCall) - .Do(_ => completion.TrySetResult(true)); + try + { + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.Any); + } + catch (ReceivedCallsException) + { + var completion = new TaskCompletionSource(); + substitute + .WhenForAnyArgs(substituteCall) + .Do(_ => completion.TrySetResult(true)); - await completion - .WaitForCompletion(timeout) - .ConfigureAwait(false); + await completion + .WaitForCompletion(timeout) + .ConfigureAwait(false); - substitute - .ValidateCallReceived( - async x => await substituteCall(x) - .ConfigureAwait(false), - MatchArgs.Any); + substitute + .ValidateCallReceived( + x => substituteCall(x), + MatchArgs.Any); + } } private static void ValidateCallReceived(