0

after lot of searches, I just can't find a way to use shim to test a private method.

here is my class:

public class MyClass()
{
        private void AddWithRefPrivate(int x, int y, ref int result)
        {
            result = x + y;
        }
}

how can I test the AddWithRefPrivate method using shims?

baruchl
  • 219
  • 2
  • 13
  • 1
    You don't. Tests should test the public API/behavior of a class. Private methods are an implementation detail. – Pete Mar 27 '14 at 19:14
  • Test only public methods that use this private methods. Think about private methods as child's of refactoring - they are just reusable parts of bigger public methods. Without them they are nonsense - can't be called, can't be used, can't do anything(they even can be dropped by compiler from resulting binary). – rufanov Mar 28 '14 at 03:32
  • possible duplicate of [What's the proper way to test a class with private methods using JUnit?](http://stackoverflow.com/questions/34571/whats-the-proper-way-to-test-a-class-with-private-methods-using-junit) – Raedwald Jul 23 '14 at 06:23

1 Answers1

1
using (ShimsContext.Create())
{
    ShimMyClass.AddWithRefPrivate =
        (int x, int y, ref int result) =>
        {
            result = x + y;
        };
}
SirH
  • 535
  • 2
  • 7
  • You can shim the private method but how you will verify it. A private method can't be called outside of that class. How you will call private method inside shim context. – Amardeep Kumar Agrawal Jul 17 '19 at 18:09