moq Mocking Behavior Mocking protected members

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

To mock a protected member you must first include the following at the top of your test fixture:

using Moq.Protected;

You then call Protected() on your mock, after which you can use the generic Setup<> with the return type of your method.

var mock = new Mock<MyClass>();
mock.Protected()
     .Setup<int>("MyProtectedGetIntMethod")
     .Returns(1);

If the method returns void then use the non-generic Setup().



Got any moq Question?