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

时间:2018-11-16 16:15:31

标签: c# fakeiteasy

我正在尝试将使用Moq编写的模拟connect('weblogic','welcome1','t3://127.0.0.1:7001') cd('AppDeployments') deplymentsList=cmo.getAppDeployments() for app in deplymentsList: domainConfig() cd ('/AppDeployments/'+app.getName()+'/Targets') mytargets = ls(returnMap='true') domainRuntime() cd('AppRuntimeStateRuntime') cd('AppRuntimeStateRuntime') for targetinst in mytargets: curstate4=cmo.getCurrentState(app.getName(),targetinst) print '----', app.getApplicationName(), ' | ', app.getVersionIdentifier(), ' | ', app.getModuleType(), ' | ', targetinst, ' | ', curstate4, ' | ', app.getSecurityDDModel(), ' | ', app.getAbsoluteSourcePath() 转换为FakeItEasy的辅助方法。 ILogger中模拟的Log()方法需要5个参数。

ILogger

似乎FakeItEasy已将参数的数量限制为4。(来自docs):

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

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

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

我应该做些不同的事情吗?很难想象有人会随意选择4作为可以传递的最大参数,所以我猜测是有原因的。 Moq的 Delegate 'Action<IFakeObjectCall>' does not take 5 arguments 没有相同的限制。

1 个答案:

答案 0 :(得分:2)

  

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