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

Viewed 29

I'm very new to xunit unitest case writing. I changed a function and also, and I made some changes in unit test case. But I got the following error. I tried to fix the issue. But no hope.

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

I got the error from below section

_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));

Actual function is *

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

The parameters in the setup need to match the ones of the method and that you use in the ReturnAsync statement. In your case, the setup matches the parameters of the method, but the lambda expression in ReturnAsync does not. If you change this to

_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));

(note the second underscore in (entityType, entityId, appId, appContext, creatorId, report, UserIds, _, _) => ...)

Related