2017-01-29 5 views
3

私のテストではたくさんのリフレクションを使用しています。 NSubstituteは、このように反射の特性(PropertyInfo)を模擬することができますNS substituteはMethodInfoの戻り値をモックできますか?

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

私はMethodInfoのために似たような方法をお教えください。

答えて

3

このような何か:

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"; 
    } 
関連する問題