Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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?

like image 799
whatupdave Avatar asked Oct 22 '09 01:10

whatupdave


2 Answers

Use a custom Do delegate that takes in the func and test that. 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.

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;
     }));;
like image 113
George Mauer Avatar answered Oct 22 '22 09:10

George Mauer


If you are not interested in the exact value of the "test" parameter, you can use Arg<> construct:

_connectionService.Stub(c => c.RemoteCall( Arg<Func<string, bool>>.Is.NotNull ))
                  .Return(true);
like image 3
Bojan Resnik Avatar answered Oct 22 '22 11:10

Bojan Resnik