From 8c5792a6fc82fea7e5d5930b7478ecdc76e58718 Mon Sep 17 00:00:00 2001 From: Chris Winland Date: Thu, 30 May 2024 09:16:15 -0400 Subject: [PATCH] Add void version of CallMethod. --- FastMoq.Core/Mocker.cs | 27 +++++++++++++++++++++++---- FastMoq.Tests/MocksTests.cs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/FastMoq.Core/Mocker.cs b/FastMoq.Core/Mocker.cs index aa878aa..4b80c87 100644 --- a/FastMoq.Core/Mocker.cs +++ b/FastMoq.Core/Mocker.cs @@ -1266,10 +1266,11 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no /// The generic "T" type is the return value type expected from the method. /// /// (CallTestMethod, 4); - /// object[] b = Mocks.CallMethod(CallTestMethod, 4, Mocks.fileSystem); - /// string c = Mocks.CallMethodS(CallStringMethod); - /// int c = Mocks.CallMethodI(CallStringMethod); + /// object[] a = Mocks.CallMethod(CallTestMethod); + /// object[] b = Mocks.CallMethod(CallTestMethod, 4); + /// object[] c = Mocks.CallMethod(CallTestMethod, 4, Mocks.fileSystem); + /// string d = Mocks.CallMethodS(CallStringMethod); + /// int e = Mocks.CallMethodI(CallStringMethod); /// ]]> /// /// Return value type. @@ -1297,6 +1298,24 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no } } + /// + /// Calls the method without needing to specify the parameters.Parameters can be specified if particular values are required. + /// + /// + /// This example shows different ways to call the method. The method can be called with or without parameters. + /// All parameters are not required, but the order does matter. + /// + /// + /// + /// The method. + /// The arguments. + /// + public void CallMethod(Delegate method, params object?[]? args) => CallMethod(method, args); + internal Dictionary CreateParamTypeDictionary(MethodBase info, params object?[]? args) { var paramList = info?.GetParameters().ToList() ?? new(); diff --git a/FastMoq.Tests/MocksTests.cs b/FastMoq.Tests/MocksTests.cs index 17334e7..5d7c785 100644 --- a/FastMoq.Tests/MocksTests.cs +++ b/FastMoq.Tests/MocksTests.cs @@ -4,6 +4,7 @@ using FastMoq.Tests.TestClasses; using System; using System.Collections.Generic; +using System.IO; using System.IO.Abstractions; using System.IO.Abstractions.TestingHelpers; using System.Linq; @@ -921,6 +922,37 @@ internal static int CallTestMethodInt(int num, IFileSystem fileSystem, ITestColl ]; } + internal void CallTestMethodVoid(int num, IFileSystem fileSystem) + { + ArgumentNullException.ThrowIfNull(fileSystem); + + if (num == 5) + { + throw new InvalidDataException(); + } + } + + [Fact] + public void CallMethod_Success_WhenVoid() + { + Mocks.CallMethod(CallTestMethodVoid); + Mocks.CallMethod(() => CallTestMethodVoid); + } + + [Fact] + public void CallMethod_Error_WhenNull() + { + Assert.Throws(() => Mocks.CallMethod(CallTestMethodVoid, 0, null)); + Assert.Throws(() => Mocks.CallMethod(CallTestMethodVoid, 0, null)); + } + + [Fact] + public void CallMethod_Error_When5() + { + Assert.Throws(() => Mocks.CallMethod(CallTestMethodVoid, 5)); + Assert.Throws(() => Mocks.CallMethod(CallTestMethodVoid, 5)); + } + [Fact] public void CallMethod() {