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 =&g开发者_如何学Pythont; 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?
This might help, though it doesn't look pretty: Rhino Mocks: Can I use Stub() when one of my parameters is Expression>?
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);
精彩评论