如何将超过 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()
没有相同的限制。
It appears that FakeItEasy has capped the number of arguments to 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()));
我正在尝试将我使用 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()
没有相同的限制。
It appears that FakeItEasy has capped the number of arguments to 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()));