2017-01-29 2 views
3

В моих тестах много отражений. NSubstitute может издеваться отражение свойств (PropertyInfo), как это:Может ли NSubstitute высмеять возврат 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"; 
    } 
Смежные вопросы