diff --git a/FastMoq.Core/Mocker.cs b/FastMoq.Core/Mocker.cs index 4b80c87..2ff1e77 100644 --- a/FastMoq.Core/Mocker.cs +++ b/FastMoq.Core/Mocker.cs @@ -786,6 +786,32 @@ public static List GetList(int count, Func? func) => GetMock().Protected() ?.Setup>(methodOrPropertyName, args ?? Array.Empty()); + /// + /// Gets the method argument data. + /// + /// The method. + /// The data. + /// System.Nullable<System.Object>[]. + ///// method + //public object?[] GetMethodArgData(MethodInfo method, Dictionary? data = null) + //{ + // if (method == null) + // { + // throw new ArgumentNullException(nameof(method)); + // } + + // var args = new List(); + + // method.GetParameters().ToList().ForEach(p => + // args.Add(data?.Any(x => x.Key == p.ParameterType) ?? false + // ? data.First(x => x.Key == p.ParameterType).Value + // : GetParameter(p.ParameterType) + // ) + // ); + + // return args.ToArray(); + //} + /// /// Gets the method argument data. /// @@ -793,7 +819,7 @@ public static List GetList(int count, Func? func) => /// The data. /// System.Nullable<System.Object>[]. /// method - public object?[] GetMethodArgData(MethodInfo method, Dictionary? data = null) + public object?[] GetMethodArgData(MethodInfo method, List>? data = null) { if (method == null) { @@ -1282,7 +1308,7 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no { ArgumentNullException.ThrowIfNull(method); - var parameters = GetMethodArgData(method.GetMethodInfo(), CreateParamTypeDictionary(method.GetMethodInfo(), args)); + var parameters = GetMethodArgData(method.GetMethodInfo(), CreateArgPairList(method.GetMethodInfo(), args)); try { return (T?) method.DynamicInvoke(parameters); @@ -1316,10 +1342,10 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no /// public void CallMethod(Delegate method, params object?[]? args) => CallMethod(method, args); - internal Dictionary CreateParamTypeDictionary(MethodBase info, params object?[]? args) + internal List> CreateArgPairList(MethodBase info, params object?[]? args) { var paramList = info?.GetParameters().ToList() ?? new(); - var newArgs = new Dictionary(); + var newArgs = new List>(); args ??= []; for (var i = 0; i < paramList.Count; i++) @@ -1333,7 +1359,7 @@ internal MockModel AddMock(Mock mock, Type type, bool overwrite = false, bool no _ => GetParameter(p.ParameterType), }; - newArgs.Add(p.ParameterType, val); + newArgs.Add(new (p.ParameterType, val)); } return newArgs; diff --git a/FastMoq.Tests/MocksTests.cs b/FastMoq.Tests/MocksTests.cs index 5d7c785..fac97ad 100644 --- a/FastMoq.Tests/MocksTests.cs +++ b/FastMoq.Tests/MocksTests.cs @@ -2,6 +2,9 @@ using FastMoq.Models; using FastMoq.Tests.TestBase; using FastMoq.Tests.TestClasses; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Moq; using System; using System.Collections.Generic; using System.IO; @@ -10,6 +13,7 @@ using System.Linq; using System.Reflection; using System.Runtime; +using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Threading; @@ -1016,13 +1020,26 @@ public void CallMethod_WithNoParamsReturnInt() result.Should().Be(0); } - [Fact] public void CallMethod_WithException() { Assert.Throws(() => Mocks.CallMethod(CallTestMethod, 4, null)); } + private static void LogException(Exception ex, ILogger log, string customMessage = "", [CallerMemberName] string caller = "") + { + log.LogError("[{caller}] - {customMessage}{errorMessage}", caller, $"{customMessage} ", ex.Message); + } + + [Fact] + public void TestLogException_CallsLogError() + { + var ex = new Exception(); + + Mocks.AddType>(); + Mocks.CallMethod(LogException, ex); + } + private void CheckBestConstructor(object data, bool expected, bool nonPublic) { var constructor = Mocks.FindConstructor(true, typeof(TestClassNormal), nonPublic);