diff --git a/src/NSubstitute/Core/WhenCalled.cs b/src/NSubstitute/Core/WhenCalled.cs index 8d956f60..35a637d8 100644 --- a/src/NSubstitute/Core/WhenCalled.cs +++ b/src/NSubstitute/Core/WhenCalled.cs @@ -1,4 +1,3 @@ -using System.Threading.Tasks; using NSubstitute.Routing; namespace NSubstitute.Core; @@ -20,15 +19,15 @@ public void Do(Action callbackWithArguments) } /// - /// Perform this action when called. - /// - /// - public void Do(Func callbackWithArguments) - { - Do(callInfo => callbackWithArguments(callInfo).GetAwaiter().GetResult()); - } + /// Perform this action when called. + /// + /// + public void Do(Func callbackWithArguments) + { + Do(callInfo => callbackWithArguments(callInfo).GetAwaiter().GetResult()); + } - /// + /// /// Perform this configured callback when called. /// /// diff --git a/src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs b/src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs index 76988cfd..464f3a9b 100644 --- a/src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs +++ b/src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Threading.Tasks; using NSubstitute.Core; using NSubstitute.Exceptions; @@ -27,7 +26,7 @@ public RouteAction Handle(ICall call) try { - (handler.DynamicInvoke(eventArguments) as Task)?.GetAwaiter().GetResult(); + (handler.DynamicInvoke(eventArguments) as Task)?.GetAwaiter().GetResult(); } catch (TargetInvocationException e) { diff --git a/tests/NSubstitute.Acceptance.Specs/Infrastructure/AnotherClass.cs b/tests/NSubstitute.Acceptance.Specs/Infrastructure/AnotherClass.cs index 99a26f64..7ef9e3f0 100644 --- a/tests/NSubstitute.Acceptance.Specs/Infrastructure/AnotherClass.cs +++ b/tests/NSubstitute.Acceptance.Specs/Infrastructure/AnotherClass.cs @@ -8,12 +8,16 @@ public abstract class AnotherClass protected abstract string ProtectedMethod(string msg, int i, char j); + protected abstract string ProtectedMethod(string msg, params int[] numbers); + protected abstract void ProtectedMethodWithNoReturn(); protected abstract void ProtectedMethodWithNoReturn(int i); protected abstract void ProtectedMethodWithNoReturn(string msg, int i, char j); + protected abstract void ProtectedMethodWithNoReturn(string msg, params int[] numbers); + public abstract void PublicVirtualMethod(); protected void ProtectedNonVirtualMethod() @@ -34,6 +38,11 @@ public string DoWork(string msg, int i, char j) return ProtectedMethod(msg, i, j); } + public string DoWork(string msg, params int[] numbers) + { + return ProtectedMethod(msg, numbers); + } + public void DoVoidWork() { ProtectedMethodWithNoReturn(); @@ -48,4 +57,9 @@ public void DoVoidWork(string msg, int i, char j) { ProtectedMethodWithNoReturn(msg, i, j); } + + public void DoVoidWork(string msg, params int[] numbers) + { + ProtectedMethodWithNoReturn(msg, numbers); + } } \ No newline at end of file diff --git a/tests/NSubstitute.Acceptance.Specs/ProtectedExtensionsTests.cs b/tests/NSubstitute.Acceptance.Specs/ProtectedExtensionsTests.cs index ba5a932b..c144d052 100644 --- a/tests/NSubstitute.Acceptance.Specs/ProtectedExtensionsTests.cs +++ b/tests/NSubstitute.Acceptance.Specs/ProtectedExtensionsTests.cs @@ -46,6 +46,19 @@ public void Should_mock_and_verify_protected_method_with_multiple_args() sub.Received(1).Protected("ProtectedMethod", Arg.Any(), Arg.Any(), Arg.Any()); } + [Test] + public void Should_mock_and_verify_protected_method_with_params_arg() + { + var expectedMsg = "unit test message"; + var sub = Substitute.For(); + var worker = new Worker(); + + sub.Protected("ProtectedMethod", Arg.Any(), Arg.Any()).Returns(expectedMsg); + + Assert.That(worker.DoWorkWithParams(sub, 3, 5), Is.EqualTo(expectedMsg)); + sub.Received(1).Protected("ProtectedMethod", Arg.Any(), Arg.Any()); + } + [Test] public void Should_throw_on_mock_null_substitute() { @@ -136,6 +149,20 @@ public void Should_mock_and_verify_void_method_with_multiple_args() sub.Received(1).Protected("ProtectedMethodWithNoReturn", Arg.Any(), Arg.Any(), Arg.Any()); } + [Test] + public void Should_mock_and_verify_void_method_with_params_arg() + { + var count = 0; + var sub = Substitute.For(); + var worker = new Worker(); + + sub.When("ProtectedMethodWithNoReturn", Arg.Any(), Arg.Any()).Do(x => count++); + + worker.DoVoidWork(sub, 6, 9); + Assert.That(count, Is.EqualTo(1)); + sub.Received(1).Protected("ProtectedMethodWithNoReturn", Arg.Any(), Arg.Any()); + } + [Test] public void Should_throw_on_void_method_null_substitute() { @@ -201,6 +228,11 @@ internal string DoEvenMoreWork(AnotherClass worker, int i, char j) return worker.DoWork("worker", i, j); } + internal string DoWorkWithParams(AnotherClass worker, params int[] numb) + { + return worker.DoWork("worker", numb); + } + internal void DoVoidWork(AnotherClass worker) { worker.DoVoidWork(); @@ -215,5 +247,10 @@ internal void DoVoidWork(AnotherClass worker, int i, char j) { worker.DoVoidWork("void worker", i, j); } + + internal void DoVoidWork(AnotherClass worker, params int[] numb) + { + worker.DoVoidWork("void worker", numb); + } } } \ No newline at end of file