Skip to content

Commit

Permalink
Fix exception bug in callmethod to return actual exception instead of…
Browse files Browse the repository at this point in the history
… wrapped exception.
  • Loading branch information
cwinland committed May 29, 2024
1 parent fa866bc commit 5a193ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 13 additions & 1 deletion FastMoq.Core/Mocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,19 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no
ArgumentNullException.ThrowIfNull(method);

var parameters = GetMethodArgData(method.GetMethodInfo(), CreateParamTypeDictionary(method.GetMethodInfo(), args));
return (T?) method.DynamicInvoke(parameters);
try
{
return (T?) method.DynamicInvoke(parameters);
}
catch (Exception ex)
{
if (ex is TargetInvocationException te && te.InnerException is { } e)
{
throw e;
}

throw;
}
}

internal Dictionary<Type, object?> CreateParamTypeDictionary(MethodBase info, params object?[]? args)
Expand Down
5 changes: 5 additions & 0 deletions FastMoq.Tests/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,11 @@ public void CallMethod_WithParams()
result[4].Should().Be("");
}

[Fact]
public void CallMethod_WithException()
{
Assert.Throws<ArgumentNullException>(() => Mocks.CallMethod<object?[]>(CallTestMethod, 4, null));
}

private void CheckBestConstructor(object data, bool expected, bool nonPublic)
{
Expand Down

0 comments on commit 5a193ce

Please sign in to comment.