Download protected method

I looked at almost all the links I can find on google for this topic, and came up with two of the following solutions that do not work correctly. I have a protected method that just returns a string.

protected virtual string ConfirmText
    {
        get
        {
            return "someTextHere";
        }
    }

This is in view mode. My tests so far, that I tried, -

[TestMethod]
    public void Confirm_Text_test()
    {
        Mock<TestViewModel> testViewModel= new Mock<TestViewModel>(null, null, null);

        testViewModel.Protected()
            .Setup<string>("ConfirmText")
            .Returns("Ok")
            .Verifiable();


        testViewModel.Verify();
    }

I understand that with the above example, I just installed and claim that I did not accept it. I could not find a way to act when setting up, for example

var result = testViewModel.ConfirmText;

as he says he is unavailable due to his level of protection.

The next way I tried is

var result = testViewModel.Object.GetType()
            .InvokeMember("ConfirmText",
             BindingFlags.InvokeMethod | 
             BindingFlags.NonPublic | 
             BindingFlags.Instance, 
             null, 
             testViewModel.Object, 
             null);

I missed something, as most of the examples I looked at show something similar to the first method I tried.

+4
1

, .

, , .

[TestMethod]
public void ConfirmText()
{
    TestViewModel testViewModel= new TestViewModel (null, null, null);

    var result = testViewModel.GetType()
    .InvokeMember("ConfirmText",
     BindingFlags.GetProperty |
     BindingFlags.NonPublic |
     BindingFlags.Instance,
     null,
     testViewModel,
     null);

    Assert.AreEqual("Confirm", result);
}

-

protected override string confirmText
{
    get
    {
        return Properties.Resources.confirmMessage;
    }
}
+2

Source: https://habr.com/ru/post/1661162/


All Articles