Reputation: 511
I have a method on an interface:
string GetUserDetails(string whatever);
I want to mock this with MOQ, so that it returns whatever the method returns i.e user details - something like:
_mock.Setup( theObject => theObject.GetUserDetails( It.IsAny<string>( ) ) )
.Returns( [object return by GetUserDetails method] ) ;
Any ideas?
Upvotes: 1
Views: 971
Reputation: 4544
For situation described in question, you need to use partial mocking. In Moq there are two different ways to achieve it:
Specifying CallBase on construction: var mock = new Mock<MyClass> { CallBase = true };
. In this case by default call on this object methods will execute real method implementation if any.
Specifying CallBase for some specific method: mock.Setup(m => m.MyMethod()).CallBase();
See also When mocking a class with Moq, how can I CallBase for just specific methods?
Upvotes: 1