Moq the result that depends on input

c#, moq, unit-testing

Solution

If `newDto(aaa)` is a call to a helper function then you could try:

processor.Setup(d => d.ProcessCommand(It.IsAny<MyCommand>))
         .Returns((MyCommand c) => new MyResponse(){ Results = new List<DtoType>(){ newDto(aaa)}});

Problem

I am a bit confused on Moq call. I need to Moq a function that takes an object. It should return a different object, whose content is generated by an existing function, that takes its input from field of the input object. So I supply a complex object in input, and one of its fields is equal to some number, and I need to reply with object that is generated taking into account that some number. I came up with the following sketch but it is full of errors. I appreciate a proper primer how to do what I need. ``` var processor = new Mock<IMyCommandProcessor>(); processor.Setup(d => d.ProcessCommand(It.IsAny<MyCommand>)) .Returns((MyResponse r) => r.Results = new List{ newDto(aaa) }); ``` newDto(aaa) is the call of a function, and instead of aaa I need a field that comes from MyCommand object given from input. How I can declare mocking here? Thanks a lot!

Original source