Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq - Need mocked function to return value passed in

Tags:

mocking

moq

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?); 
like image 655
Rod Johnson Avatar asked Oct 14 '10 19:10

Rod Johnson


People also ask

How does MOQ mock work?

Mock objects allow you to mimic the behavior of classes and interfaces, letting the code in the test interact with them as if they were real. This isolates the code you're testing, ensuring that it works on its own and that no other code will make the tests fail.


1 Answers

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

like image 56
Jakub Konecki Avatar answered Oct 02 '22 11:10

Jakub Konecki