如何在Rhino Mocks中存根Func <T,TResult>?

wha*_*ave 5 c# rhino-mocks

我有一个依赖的类:

private readonly IWcfClient<ITestConnectionService> _connectionClient;
Run Code Online (Sandbox Code Playgroud)

我想要发出这个电话:

_connectionClient.RemoteCall(client => client.Execute("test"));
Run Code Online (Sandbox Code Playgroud)

这目前无效:

_connectionService
    .Stub(c => c.RemoteCall(rc => rc.Execute("test")))
    .Return(true);
Run Code Online (Sandbox Code Playgroud)

这在Rhino有可能吗?

Geo*_*uer 3

使用接受 func 的自定义 Do 委托并对其进行测试。您可以通过将其转换为表达式并解析表达式树来完成此操作,或者仅使用模拟输入运行委托并测试结果。

如果 RemoteCall() 内的 lambda 不包含 x=>x.Execute("test"),则以下内容将引发错误 - 您可以按照这个想法让它完全按照您的意愿行事。

public interface IExecute {
  void Execute(string input)
}
_connectionService
    .Stub(c => c.RemoteCall(null)).IgnoreArguments()
    .Do(new Func<Action<IExecute>,bool>( func => {
       var stub = MockRepository.GenerateStub<IExecute>();
       func(stub);
       stub.AssertWasCalled(x => x.Execute("test"));
       return true;
     }));;
Run Code Online (Sandbox Code Playgroud)