How to ask RhinoMock to correctly expect a Lambda expression

1.1k Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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);
0
On

This might help, though it doesn't look pretty: Rhino Mocks: Can I use Stub() when one of my parameters is Expression>?