Search code examples
c#rhino-mocksanonymous-methods

Rhino Mocks: How to return conditional result from a mock object method


I would like to do something like the following but can't seem to get the syntax for the Do method quite right.

var sqr = new _mocks.CreateRenderer<ShapeRenderer>();
Expect.Call(sqr.CanRender(null)).IgnoreArguments().Do(x =>x.GetType() == typeof(Square)).Repeat.Any();

So basically, I would like to set up the sqr.CanRender() method to return true if the input is of type Square and false otherwise.


Solution

  • Are you looking for this?

    Expect.Call(sqr.CanRender(null)).IgnoreArguments()
        .Do((Func<Shape, bool>) delegate(Agent x){return x.GetType() == typeof(Square);})
        .Repeat.Any();
    

    EDIT: The answer was correct in spirit bu the original syntax did not quite work.