Skip to content

Instantly share code, notes, and snippets.

@ramonjoaquim
Last active July 21, 2020 16:00
Show Gist options
  • Save ramonjoaquim/fae014f546dd7c00e0dd06c979a3dc7a to your computer and use it in GitHub Desktop.
Save ramonjoaquim/fae014f546dd7c00e0dd06c979a3dc7a to your computer and use it in GitHub Desktop.
invoke private method to mock
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Moq;
namespace TestExamples01.Setup
{
public class Setup
{
public static object InvokePrivateMethod<T>(object classInstance, string nameMethod, object[] parameters) where T : class
{
var methodInfo = typeof(T).GetMethod(nameMethod, BindingFlags.NonPublic | BindingFlags.Instance);
return methodInfo?.Invoke(classInstance, parameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment