3

我有一个接口,它有一个名为:

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

我想要这种方法Setup并实际调用. 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)

知道我做错了什么吗?

4

1 回答 1

3

你调用了错误的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))
    );
于 2012-10-24T19:26:54.553 回答