2010-10-14 77 views
31

我有一個模擬,我已經設置這樣。我需要返回中傳遞到.CreatePersonNameMoq - 需要模擬函數返回值

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

回答

45
mock.Setup(m => m.CreatePersonName(It.IsAny<PersonName>())) 
      .Returns((PersonName p) => p); 

基於相同的價值:

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

http://code.google.com/p/moq/wiki/QuickStart

+0

非常感謝你 – 2010-10-14 20:26:20

+0

代碼現在遷移到github上:HTTPS ://github.com/Moq/moq4/wiki/Quickstart – miltonb 2017-03-26 21:03:45