Moq - Need mocked function to return value passed in

Rod Johnson picture Rod Johnson · Oct 14, 2010 · Viewed 21.6k times · Source

I have a mock that i have setup like this. I need to return the same value that was passed in to .CreatePersonName

mock.Setup(m => m.CreatePersonName(It.IsAny<PersonName>()))
            .Returns(// what do i put here?);

Answer

Jakub Konecki picture Jakub Konecki · Oct 14, 2010
mock.Setup(m => m.CreatePersonName(It.IsAny<PersonName>()))
            .Returns((PersonName p) => p);

Based on:

// access invocation arguments when returning a value
mock.Setup(x => x.DoSomething(It.IsAny<string>()))
                .Returns((string s) => s.ToLower());

from https://github.com/Moq/moq4/wiki/Quickstart