繁体   English   中英

如何将超过4个参数传递给FakeItEasy Invokes()?

[英]How to pass more than 4 arguments to FakeItEasy Invokes()?

我正在尝试将使用Moq编写的模拟ILogger编写的辅助方法转换为FakeItEasy。 ILogger模拟的Log()方法需要5个参数。

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

似乎FakeItEasy已将参数的数量限制为4。(来自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);

因此,当我编写这段代码时...

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

...我收到以下错误

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

我应该做些不同的事情吗? 很难想象有人会随意选择4作为可以传递的最大参数,所以我猜测是有原因的。 Moq的Callback()没有相同的限制。

似乎FakeItEasy已将参数数量限制为4。

并不是的。 有多达4个参数的辅助程序重载,但实际上可以有任意数量的参数,尽管语法不太方便:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM