Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/NSubstitute/Core/WhenCalled.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Threading.Tasks;
using NSubstitute.Routing;

namespace NSubstitute.Core;
Expand All @@ -20,15 +19,15 @@ public void Do(Action<CallInfo> callbackWithArguments)
}

/// <summary>
/// Perform this action when called.
/// </summary>
/// <param name="callbackWithArguments"></param>
public void Do(Func<CallInfo, Task> callbackWithArguments)
{
Do(callInfo => callbackWithArguments(callInfo).GetAwaiter().GetResult());
}
/// Perform this action when called.
/// </summary>
/// <param name="callbackWithArguments"></param>
public void Do(Func<CallInfo, Task> callbackWithArguments)
{
Do(callInfo => callbackWithArguments(callInfo).GetAwaiter().GetResult());
}

/// <summary>
/// <summary>
/// Perform this configured callback when called.
/// </summary>
/// <param name="callback"></param>
Expand Down
3 changes: 1 addition & 2 deletions src/NSubstitute/Routing/Handlers/RaiseEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Threading.Tasks;
using NSubstitute.Core;
using NSubstitute.Exceptions;

Expand Down Expand Up @@ -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)
{
Expand Down
14 changes: 14 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/Infrastructure/AnotherClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand All @@ -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);
}
}
37 changes: 37 additions & 0 deletions tests/NSubstitute.Acceptance.Specs/ProtectedExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ public void Should_mock_and_verify_protected_method_with_multiple_args()
sub.Received(1).Protected("ProtectedMethod", Arg.Any<string>(), Arg.Any<int>(), Arg.Any<char>());
}

[Test]
public void Should_mock_and_verify_protected_method_with_params_arg()
{
var expectedMsg = "unit test message";
var sub = Substitute.For<AnotherClass>();
var worker = new Worker();

sub.Protected("ProtectedMethod", Arg.Any<string>(), Arg.Any<int[]>()).Returns(expectedMsg);

Assert.That(worker.DoWorkWithParams(sub, 3, 5), Is.EqualTo(expectedMsg));
sub.Received(1).Protected("ProtectedMethod", Arg.Any<string>(), Arg.Any<int[]>());
}

[Test]
public void Should_throw_on_mock_null_substitute()
{
Expand Down Expand Up @@ -136,6 +149,20 @@ public void Should_mock_and_verify_void_method_with_multiple_args()
sub.Received(1).Protected("ProtectedMethodWithNoReturn", Arg.Any<string>(), Arg.Any<int>(), Arg.Any<char>());
}

[Test]
public void Should_mock_and_verify_void_method_with_params_arg()
{
var count = 0;
var sub = Substitute.For<AnotherClass>();
var worker = new Worker();

sub.When("ProtectedMethodWithNoReturn", Arg.Any<string>(), Arg.Any<int[]>()).Do(x => count++);

worker.DoVoidWork(sub, 6, 9);
Assert.That(count, Is.EqualTo(1));
sub.Received(1).Protected("ProtectedMethodWithNoReturn", Arg.Any<string>(), Arg.Any<int[]>());
}

[Test]
public void Should_throw_on_void_method_null_substitute()
{
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}
}