Xunit, MOCK unitest error : Setup on method with 9 parameter(s) cannot invoke callback with different number of parameters (8)

Xunit, MOCK unitest error : Setup on method with 9 parameter(s) cannot invoke callback with different number of parameters (8)

我对 xunit unitest 案例写作还很陌生。我还更改了一个功能,并且对单元测试用例进行了一些更改。但是我收到以下错误。我试图解决这个问题。但没有希望。

System.ArgumentException : Invalid callback. Setup on method with 9 parameter(s) cannot invoke callback with different number of parameters (8).

Stack Trace: 
MethodCall.<SetReturnsResponse>g__ValidateCallback|22_1(Delegate callback)
MethodCall.SetReturnsResponse(Delegate valueFactory)
NonVoidSetupPhrase`2.Returns[T1,T2,T3,T4,T5,T6,T7,T8](Func`9 valueExpression)
GeneratedReturnsExtensions.ReturnsAsync[T1,T2,T3,T4,T5,T6,T7,T8,TMock,TResult](IReturns`2 mock, Func`9 valueFunction)
CreateReportTests.ctor() line 39

我从下面的部分得到了错误

_ReportClientMock
                .Setup(m => m.CreateReportAsync(
                It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
                It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<IEnumerable<Guid>>(), It.IsAny<TimeSpan>(), It.IsAny<bool>()))

                .ReturnsAsync<string, string, string, string, Guid, string, IEnumerable<Guid>, TimeSpan, IReportClient, Report>
                ((entityType, entityId, appId, appContext, creatorId, report, UserIds, _) =>

               new Report(Guid.NewGuid(), entityType, entityId, appId, appContext, creatorId, DateTimeOffset.UtcNow, null, report, false, UserIds));

实际函数是*

Task<Comment> CreateReportAsync(string entityType, string entityId, string appId, string? appContext, Guid creatorId, string report, IEnumerable<Guid>? UserIds, TimeSpan expirationTime, bool allow);

设置中的参数需要与方法中的参数以及您在 ReturnAsync 语句中使用的参数相匹配。在您的情况下,设置与方法的参数匹配,但 ReturnAsync 中的 lambda 表达式不匹配。如果将其更改为

_ReportClientMock
  .Setup(m => m.CreateReportAsync(
    It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(),
    It.IsAny<Guid>(), It.IsAny<string>(), It.IsAny<IEnumerable<Guid>>(), It.IsAny<TimeSpan>(), It.IsAny<bool>()))
  .ReturnsAsync<string, string, string, string, Guid, string, IEnumerable<Guid>, TimeSpan, bool>
    ((entityType, entityId, appId, appContext, creatorId, report, UserIds, _, _) =>
           new Report(Guid.NewGuid(), entityType, entityId, appId, appContext, creatorId, DateTimeOffset.UtcNow, null, report, false, UserIds));

(注意(entityType, entityId, appId, appContext, creatorId, report, UserIds, _, _) => ...中的第二个下划线)