简体   繁体   English

我如何存根Func <T,TResult> 在犀牛Mo?

[英]How do I stub a Func<T,TResult> in Rhino Mocks?

I have a class with a dependency: 我有一个依赖项的类:

private readonly IWcfClient<ITestConnectionService> _connectionClient;

and I want to stub out this call: 我想打断这个电话:

_connectionClient.RemoteCall(client => client.Execute("test"));

This currently isn't working: 目前无法使用:

_connectionService
    .Stub(c => c.RemoteCall(rc => rc.Execute("test")))
    .Return(true);

Is this possible in Rhino? 犀牛有可能吗?

If you are not interested in the exact value of the "test" parameter, you can use Arg<> construct: 如果您对“ test”参数的确切值不感兴趣,则可以使用Arg<>构造:

_connectionService.Stub(c => c.RemoteCall( Arg<Func<string, bool>>.Is.NotNull ))
                  .Return(true);

Use a custom Do delegate that takes in the func and test that. 使用接受功能的自定义Do委托并进行测试。 You can do it by converting it to an expression and parsing the expression tree, or just run the delegate with a mock input and test the results. 您可以通过将其转换为表达式并解析表达式树来完成此操作,或者仅使用模拟输入运行委托并测试结果。

The following will throw an error if the lambda inside RemoteCall() does not contain x=>x.Execute("test") - you can work off of the idea to get it to do exactly what you want. 如果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;
     }));;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM