Skip to content

Commit

Permalink
Add void version of CallMethod.
Browse files Browse the repository at this point in the history
  • Loading branch information
cwinland committed May 30, 2024
1 parent 8e1506a commit 8c5792a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
27 changes: 23 additions & 4 deletions FastMoq.Core/Mocker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// <code>
/// <![CDATA[
/// object[] a = Mocks.CallMethod<object[]>(CallTestMethod, 4);
/// object[] b = Mocks.CallMethod<object?[]>(CallTestMethod, 4, Mocks.fileSystem);
/// string c = Mocks.CallMethodS<string>(CallStringMethod);
/// int c = Mocks.CallMethodI<int>(CallStringMethod);
/// object[] a = Mocks.CallMethod<object[]>(CallTestMethod);
/// object[] b = Mocks.CallMethod<object[]>(CallTestMethod, 4);
/// object[] c = Mocks.CallMethod<object?[]>(CallTestMethod, 4, Mocks.fileSystem);
/// string d = Mocks.CallMethodS<string>(CallStringMethod);
/// int e = Mocks.CallMethodI<int>(CallStringMethod);
/// ]]></code>
/// </example>
/// <typeparam name="T">Return value type.</typeparam>
Expand Down Expand Up @@ -1297,6 +1298,24 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no
}
}

/// <summary>
/// Calls the method without needing to specify the parameters.Parameters can be specified if particular values are required.
/// </summary>
/// <example>
/// 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.
/// <code>
/// <![CDATA[
/// Mocks.CallMethod(CallTestMethod);
/// Mocks.CallMethod(CallTestMethod, 4);
/// Mocks.CallMethod(CallTestMethod, 4, Mocks.fileSystem);
/// ]]></code>
/// </example>
/// <param name="method">The method.</param>
/// <param name="args">The arguments.</param>
/// <exception cref="System.ArgumentNullException" />
public void CallMethod(Delegate method, params object?[]? args) => CallMethod<object>(method, args);

internal Dictionary<Type, object?> CreateParamTypeDictionary(MethodBase info, params object?[]? args)
{
var paramList = info?.GetParameters().ToList() ?? new();
Expand Down
32 changes: 32 additions & 0 deletions FastMoq.Tests/MocksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<object>(CallTestMethodVoid);
Mocks.CallMethod(() => CallTestMethodVoid);
}

[Fact]
public void CallMethod_Error_WhenNull()
{
Assert.Throws<ArgumentNullException>(() => Mocks.CallMethod<object>(CallTestMethodVoid, 0, null));
Assert.Throws<ArgumentNullException>(() => Mocks.CallMethod(CallTestMethodVoid, 0, null));
}

[Fact]
public void CallMethod_Error_When5()
{
Assert.Throws<InvalidDataException>(() => Mocks.CallMethod<object>(CallTestMethodVoid, 5));
Assert.Throws<InvalidDataException>(() => Mocks.CallMethod(CallTestMethodVoid, 5));
}

[Fact]
public void CallMethod()
{
Expand Down

0 comments on commit 8c5792a

Please sign in to comment.