From 5a193cea636c5d3502ab0e0c6a46e9f9acd49aaa Mon Sep 17 00:00:00 2001 From: Chris Winland Date: Wed, 29 May 2024 09:27:16 -0400 Subject: [PATCH] Fix exception bug in callmethod to return actual exception instead of wrapped exception. --- FastMoq.Core/Mocker.cs | 14 +++++++++++++- FastMoq.Tests/MocksTests.cs | 5 +++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/FastMoq.Core/Mocker.cs b/FastMoq.Core/Mocker.cs index 16b0f8c..9b0f566 100644 --- a/FastMoq.Core/Mocker.cs +++ b/FastMoq.Core/Mocker.cs @@ -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 CreateParamTypeDictionary(MethodBase info, params object?[]? args) diff --git a/FastMoq.Tests/MocksTests.cs b/FastMoq.Tests/MocksTests.cs index a6d72a9..14b3c6b 100644 --- a/FastMoq.Tests/MocksTests.cs +++ b/FastMoq.Tests/MocksTests.cs @@ -938,6 +938,11 @@ public void CallMethod_WithParams() result[4].Should().Be(""); } + [Fact] + public void CallMethod_WithException() + { + Assert.Throws(() => Mocks.CallMethod(CallTestMethod, 4, null)); + } private void CheckBestConstructor(object data, bool expected, bool nonPublic) {