none
Mocking of Private Methods and Fields in Unit Test RRS feed

  • Question

  • Hi All,

    I want to mock private fields and methods for unit test.

    What is the best way to do it.? I tried PrivateObject class but I am getting MethodMissingException.

    Any help is appreciated.

    Thursday, October 17, 2019 5:29 AM

All replies

  • Hi sud89,

    Thank you for posting here.

    For your requirement, we recommend you could extract your private method to a specific class and test this class. Please refer this sample:

    Original:

    public class Order
    {
        private Customer _customer;
        private List<Product> _products;
    
        public string GenerateDescription()
        {
            return $"Customer name: {_customer.Name}, " +
                $"total number of products: {_products.Count}, " +
                $"total price: {GetPrice()}";
        }
    
        private decimal GetPrice()
        {
            decimal basePrice = /* Calculate based on _products */;
            decimal discounts = /* Calculate based on _customer */;
            decimal taxes = /* Calculate based on _products */;
            return basePrice - discounts + taxes;
        }
    }
    

    Testing:

    public class Order
    {
        private Customer _customer;
        private List<Product> _products;
    
        public string GenerateDescription()
        {
            var calculator = new PriceCalculator();
    
            return $"Customer name: {_customer.Name}, " +
                $"total number of products: {_products.Count}, " +
                $"total price: {calculator.Calculate(_customer, _products)}";
        }
    }
    
    //a separate domain concept
    public class PriceCalculator
    {
        public decimal Calculate(Customer customer, List<Product> products)
        {
            decimal basePrice = /* Calculate based on products */;
            decimal discounts = /* Calculate based on customer */;
            decimal taxes = /* Calculate based on products */;
            return basePrice - discounts + taxes;
        }
    }

    This class itself doesn’t maintain any internal state: it generates the output based on the provided input. And you could refer more details here: Unit testing private methods

    Hope it could help you.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, October 17, 2019 10:46 AM
  • Hi sud89,

    Sorry for bothering you.

    Could the reply help you test private methods and fields in unit test? If not, please feel free to contact us, we would try our best to provide a solution.

    Look forward to your feedback.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, October 22, 2019 8:11 AM
  • Hi Dylan,

    Yes your post helped me in some extent.

    https://enterprisecraftsmanship.com/posts/unit-testing-private-methods/

    Above link explains Unit testing of private methods that made my work easy in some extent. I need to relate the things with my scenarios those I'm looking for.

    Actually I was looking for Unit testing of private methods using MOCK framework or Microsoft Fakes.

    Regards,

    Sudhir

    Tuesday, October 22, 2019 8:17 AM
  • Hi sud89,

    Thank you for feedback.

    After my research, I found some similar SO threads about this, it seems that it doesn't support mocking private method in unit test currently.

    How to write unit test for private method in c# using moq framework?

    C# Unit testing - check if a private method was reached 

    Hope it could help you.

    Cheers,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, October 22, 2019 8:53 AM