Skip to content

Commit 54ca9b4

Browse files
Add helper methods for testing private methods
1 parent cda0d8e commit 54ca9b4

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

Src/Tests/Helper.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Tests
1313
{
14-
public class Helper
14+
public static class Helper
1515
{
1616
private static readonly Calc _calc = new();
1717
public static Calc Calc => _calc;
@@ -49,6 +49,30 @@ public static void ComparePrivateField<InstanceType, FieldType>(InstanceType ins
4949
}
5050
}
5151

52+
public static void CallPrivateMethod<InstanceType>(this InstanceType instance, string methodName, params object[] parameters)
53+
{
54+
Type type = instance.GetType();
55+
BindingFlags bindingAttr = BindingFlags.NonPublic | BindingFlags.Instance;
56+
MethodInfo method = type.GetMethod(methodName, bindingAttr);
57+
if (method is null)
58+
{
59+
Assert.Fail("Method was not found.");
60+
}
61+
method.Invoke(instance, parameters);
62+
}
63+
64+
public static TReturn CallPrivateMethod<InstanceType, TReturn>(this InstanceType instance, string methodName, params object[] parameters)
65+
{
66+
Type type = instance.GetType();
67+
BindingFlags bindingAttr = BindingFlags.NonPublic | BindingFlags.Instance;
68+
MethodInfo method = type.GetMethod(methodName, bindingAttr);
69+
if (method is null)
70+
{
71+
Assert.Fail("Method was not found.");
72+
}
73+
return (TReturn)method.Invoke(instance, parameters);
74+
}
75+
5276
public static string ReadResources(string resourceName, string fileExtention = "json")
5377
{
5478
Assembly asm = Assembly.GetExecutingAssembly();

0 commit comments

Comments
 (0)