Moq – Getting the Value Of a Mocked Method Parameter

moq logo

There’s scenarios where we don’t care about the Moq “It.IsAny<>” parameter value being passed into a mocked method but we need to get the value on the way out. For example, take the scenario below. I’m using SQLite to form integration tests and I need to get a handle on the generated ParentWidget.Id to tie back to my Widget FK.

var dbWidget = _mapper.Map(widget);
await _ctx.Widgets.AddAsync(dbWidget);

mockedService.Setup(v => v.CreationOfParentMethod(It.IsAny(), It.IsAny()))
                          .Returns((ParentWidget p, Widget w) => 
                          { myAutoFixureObj.ParentWidgetId = r.Id; return myAutoFixureObj});

For some reason I thought I’d need to break out to an anonymous callback method but it’s pretty simple. My continuing series on Moq.

Leave a Reply

%d bloggers like this: