.net 如何调用Moq中作为参数传递的谓词?
我有一个接口,它有一个名为:.net 如何调用Moq中作为参数传递的谓词?,.net,unit-testing,mocking,moq,.net,Unit Testing,Mocking,Moq,我有一个接口,它有一个名为: IEnumerable<string> GetFilesInADirectoryWhere( string directory, string wildcard, Predicate<string> filter); 这会编译,但当调用此模拟方法时,会出现以下运行时异常: SetUp : System.Reflection.TargetParameterCountException : Parame
IEnumerable<string> GetFilesInADirectoryWhere(
string directory,
string wildcard,
Predicate<string> filter);
这会编译,但当调用此模拟方法时,会出现以下运行时异常:
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)
知道我做错了什么吗?你调用了错误的
返回重载。应该是:
返回(Func)
-匹配方法的签名
安装程序应如下所示:
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))
);
myMock
.Setup(m=>m.getfilesindirectorywhere(
@“我的文件夹”,
@“FooFile**”,
It.IsAny())
)
.返回((目录、通配符、筛选器)=>
cannedFileNames.Where(文件名=>过滤器(文件名))
);
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))
);