gpt4 book ai didi

.net - 如何在 Moq 中调用作为参数传递的谓词?

转载 作者:行者123 更新时间:2023-12-02 13:50:21 25 4
gpt4 key购买 nike

我有一个接口(interface),其中有一个名为:

IEnumerable<string> GetFilesInADirectoryWhere(
string directory,
string wildcard,
Predicate<string> filter);

我想设置此方法并实际调用过滤器。所以我这样声明:

myMock.Setup( x => x.GetFilesInADirectoryWhere(
@"My Folder",
@"FooFile*.*",
It.IsAny<Predicate<string>>()))

.Returns((Predicate<string> filter) =>
cannedFileNames.Where(filename=>filter(filename)));

这可以编译,但是当调用这个模拟方法时,我得到以下运行时异常:

    SetUp : System.Reflection.TargetParameterCountException : Parameter count mismatch.
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at Moq.Extensions.InvokePreserveStack(Delegate del, Object[] args)
at Moq.MethodCallReturn`2.Execute(ICallContext call)
at Moq.Interceptor.Intercept(ICallContext invocation)
at Moq.Proxy.CastleProxyFactory.Interceptor.Intercept(IInvocation invocation)
at Castle.DynamicProxy.AbstractInvocation.Proceed()
at Castle.Proxies.IDiskProxy.GetFilesInADirectoryWhere(String directory, String filter, Predicate`1 predicate)

知道我做错了什么吗?

最佳答案

您调用了错误的Returns重载。应该是:

  • Returns<T1,T2,T3>(Func<T1,T2,T3,IEnumerable<string>>) - 匹配你的方法的签名

设置应如下所示:

myMock
.Setup(m => m.GetFilesInADirectoryWhere(
@"My Folder",
@"FooFile*.*",
It.IsAny<Predicate<string>>())
)
.Returns<string, string, Predicate<string>>((dir, wildcard, filter) =>
cannedFileNames.Where(filename => filter(filename))
);

关于.net - 如何在 Moq 中调用作为参数传递的谓词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13053977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com