Search code examples
.netlambdarhino-mocks

How to ask RhinoMock to correctly expect a Lambda expression


I am using Rhino Mocks and I'm not sure how to mock a call that takes a lambda expression. Here's the situation:

Actual Method:

public void MyMethod (int subtestId) {
var interview = _repository.FindOne(t => t.Survey.Subtests.SingleOrDefault(x => x.Id == subtestId) != null);
...content elided...
}

Mock attempt:

var interview = new Interview();
_repository.Expect(r => r.FindOne(t => t.Survey.Subtests.SingleOrDefault(x => x.Id == subtestId) != null)).Return(interview);

 var viewModelRetrieved =  _service.MyMethod(subtestId);

When I run this and step through, var interview in MyMethod gets set to null. The subtestId value is correct.

Is there another way to do this?


Solution

  • This is maybe not the answer exactly, but what ended up working for me was to use IgnoreArguments() like so:

       var interview = new Interview();
        _repository
    .Expect(r => r
           .FindOne(t => t.Survey.Subtests.SingleOrDefault(x => x.Id == subtestId) != null))
    .IgnoreArguments()
    .Return(interview);
    
         var viewModelRetrieved =  _service.MyMethod(subtestId);