Using Moq to mock only some methods

This is called a partial mock, and the way I know to do it in Moq requires mocking the class rather than the interface and then setting the “Callbase” property on your mocked object to “true”.

This will require making all the methods and properties of the class you are testing virtual. Assuming this isn’t a problem, you can then write a test like this:

var mock = new Mock<YourTestClass>();
mock.CallBase = true;
mock.Setup(x => x.GetCurrentUser()).Returns(lUnauthorizedUser);
mockedTest.Object.MyMethod();

Leave a Comment