1

I am trying to convert a helper method I wrote mocking ILogger using Moq to FakeItEasy. The Log() method mocked in the ILogger requires 5 arguments.

Log(LogLevel, EventId, FormattedLogValues, Exception, Func<object, Exception, string>)

It appears that FakeItEasy has capped the number of arguments to 4. (From the docs):

// Pass up to 4 original call argument values into the method that creates the exception.
A.CallTo(()=>fakeShop.NumberOfSweetsSoldOn(A<DateTime>._))
  .Invokes((DateTime when) => System.Console.Out.WriteLine("showing sweet sales for " + when))
  .Returns(17);

Therefore when I write this code...

var logs = new List<string>();
var logger = A.Fake<ILogger<ElasticSearchRepository>>();
A.CallTo(() => logger.Log(A<LogLevel>._, A<EventId>._, A<FormattedLogValues>._, A<Exception>._, A<Func<object, Exception, string>>._))
    .Invokes((LogLevel a, EventId b, FormattedLogValues x, Exception c, Func<object, Exception, string> d) => logs.Add(x.ToString()));

... I get the following error

 Delegate 'Action<IFakeObjectCall>' does not take 5 arguments

Is there something I should be doing differently? It's hard to imagine anyone arbitrarily picking 4 as the max arguments that can be passed so I'm guessing there is a reason. Moq's Callback() doesn't have the same limitations.

Sailing Judo
  • 11,083
  • 20
  • 66
  • 97
  • I had a quick look at the code on github and it seems to be the case, some overloads of functions are only defined up to 4 arguments. – Schwarzie2478 Nov 16 '18 at 16:29
  • Not a 100% duplicate but I guess at least related: [FakeItEasy ReturnLazily with more than 4 arguments method](https://stackoverflow.com/q/31018199/1220550) – Peter B Nov 16 '18 at 16:34

1 Answers1

2

It appears that FakeItEasy has capped the number of arguments to 4.

Not really. There are helper overloads for up to 4 arguments, but you can actually have any number of arguments, albeit with a less convenient syntax:

A.CallTo(() => logger.Log(A<LogLevel>._, A<EventId>._, A<FormattedLogValues>._, A<Exception>._, A<Func<object, Exception, string>>._))
    .Invokes(call => logs.Add(call.GetArgument<FormattedLogValues>("state").ToString()));
Sailing Judo
  • 11,083
  • 20
  • 66
  • 97
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758