mayank gupta
mayank gupta

Reputation: 511

MOQ - Returning value that was return by method

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

Answers (1)

arghtype
arghtype

Reputation: 4544

For situation described in question, you need to use partial mocking. In Moq there are two different ways to achieve it:

  1. 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.

  2. 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

Related Questions