Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:44:31+00:00 2026-05-15T04:44:31+00:00

I have seen many posts and questions about Mocking a private method but still

  • 0

I have seen many posts and questions about “Mocking a private method” but still cannot make it work and not found a real answer.
Lets forget the code smell and you should not do it etc….

From what I understand I have done the following:

1) Created a class Library “MyMoqSamples”

2) Added a ref to Moq and NUnit

3) Edited the AssemblyInfo file and added
[assembly: InternalsVisibleTo(“DynamicProxyGenAssembly2”)]
[assembly: InternalsVisibleTo(“MyMoqSamples”)]

4) Now need to test a private method.Since it’s a private method it’s not part of an interface.

5) added the following code

[TestFixture]
public class Can_test_my_private_method
{
    [Test]
    public void Should_be_able_to_test_my_private_method()
    {
        // TODO how do I test my DoSomthing method?
    }
}

public class CustomerInfo
{
    public string Name { get; set; }
    public string Surname { get; set; }
}

public interface ICustomerService
{
    List<CustomerInfo> GetCustomers();
}

public class CustomerService : ICustomerService
{
    public List<CustomerInfo> GetCustomers()
    {
        return new List<CustomerInfo> { new CustomerInfo { Surname = "Bloggs", Name = "Jo" } };
    }

    protected virtual void DoSomething()
    {
    }
}

Could you provide me an example on how you would test my private method?
Thanks a lot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:44:32+00:00Added an answer on May 15, 2026 at 4:44 am

    The steps you’re describing set Moq up to test internal classes and members so have nothing really to do with testing a protected or private method

    Testing private methods is a bit of a smell, you should really test just the public API. If you feel that the method is really important and needs to be tested in isolation perhaps it deserves to be in its own class where it can then be tested on its own?

    If your heart is set on testing the protected method above you can roll your own Mock in your test assembly:

    public class CustomerServiceMock : CustomerService {
        public void DoSomethingTester() {
             // Set up state or whatever you need
             DoSomething();
        }
    
    }
    
    [TestMethod]
    public void DoSomething_WhenCalled_DoesSomething() {
        CustomerServiceMock serviceMock = new CustomerServiceMock(...);
        serviceMock.DoSomethingTester();
     }
    

    If it was private you could probably do something dodgy with reflection but going that route is the way to testing hell.


    Update

    While you’ve given sample code in your question I don’t really see how you want to “test” the protected method so I’ll come up with something contrived…

    Lets say your customer service looks like this:-

     public CustomerService : ICustomerService {
    
          private readonly ICustomerRepository _repository;
    
          public CustomerService(ICustomerRepository repository) {
               _repository = repository;
          } 
    
          public void MakeCustomerPreferred(Customer preferred) {
               MakePreferred(customer);
               _repository.Save(customer);
          }
    
          protected virtual void MakePreferred(Customer customer) {
              // Or more than likely some grungy logic
              customer.IsPreferred = true;
          }
     }
    

    If you wanted to test the protected method you can just do something like:-

    [TestClass]
    public class CustomerServiceTests {
    
         CustomerServiceTester customerService;
         Mock<ICustomerRepository> customerRepositoryMock;
    
         [TestInitialize]
         public void Setup() {
              customerRepoMock = new Mock<ICustomerRepository>();
              customerService = new CustomerServiceTester(customerRepoMock.Object);
         }
    
    
         public class CustomerServiceTester : CustomerService {    
              public void MakePreferredTest(Customer customer) {
                  MakePreferred(customer);
              }
    
              // You could also add in test specific instrumentation
              // by overriding MakePreferred here like so...
    
              protected override void MakePreferred(Customer customer) {
                  CustomerArgument = customer;
                  WasCalled = true;
                  base.MakePreferred(customer);
              }
    
              public Customer CustomerArgument { get; set; }
              public bool WasCalled { get; set; }
         }
    
         [TestMethod]
         public void MakePreferred_WithValidCustomer_MakesCustomerPreferred() {
             Customer customer = new Customer();
             customerService.MakePreferredTest(customer);
             Assert.AreEqual(true, customer.IsPreferred);
         }
    
         // Rest of your tests
    }
    

    The name of this “pattern” is Test specific subclass (based on xUnit test patterns terminology) for more info you might want to see here:-

    http://xunitpatterns.com/Test-Specific%20Subclass.html

    Based on your comments and previous question it seems like you’ve been tasked with implementing unit tests on some legacy code (or made the decision yourself). In which case the bible of all things legacy code is the book by Michael Feathers. It covers techniques like this as well as refactorings and techniques to deal with breaking down “untestable” classes and methods into something more manageable and I highly recommend it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.