NSubstitute 可以模拟 MethodInfo 的 return 吗?

Can NSubstitute mock the return of a MethodInfo?

我的测试使用了很多反射。 NSubstitute 可以像这样模拟反射属性(PropertyInfo):

mock
.GetType().GetTypeInfo()
.GetProperty("SomePropertyName")
.GetValue(mock)
.Returns(someReturnValue);   // NSubstitute does its thing here

如何为 MethodInfo 做类似的事情?

像这样:

  internal class Program
  {
    private static void Main()
    {
      var mock = Substitute.For<SomeClass>();
      var mi = mock.GetType().GetTypeInfo()
        .GetMethod("SomeMethod", BindingFlags.NonPublic | BindingFlags.Instance);

      mi.Invoke(mock, null).Returns("xxxxXXX");

      Console.WriteLine(mi.Invoke(mock, null)); // -> Write xxxxXXX
    }
  }

  public class SomeClass
  {
    protected virtual string SomePropertyName { get; set; }

    protected virtual string SomeMethod() => "aaa";
  }