How can I use PrivateObject to access private members of both my class and its parent?

前端 未结 7 1914
逝去的感伤
逝去的感伤 2020-12-08 07:41

I\'m testing a class that is part of a hierarchy. I\'ve been setting up my test classes with the object under test, and a PrivateObject to allow access to that

相关标签:
7条回答
  • 2020-12-08 08:15

    This likely isn't the answer you want...but you shouldn't be testing both classes in one method in the first place. You should only ever be testing one class at a time. If you feel the need to do this, then I'd guess that your code needs refactoring. But as I don't know your real-life code problem, I can't say for sure

    0 讨论(0)
  • 2020-12-08 08:16

    I didn't find the answer, so this is what I ended up doing. I created PrivateObjects for each level of the class's hierarchy, and I just need to be careful when writing test cases that I use the proper one.

    public class BaseClass
    {
        private int one = 1;
    }
    
    public class SubClass : BaseClass
    {
        private int two = 2;
    }
    
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod()
        {
            SubClass test = new SubClass();
            PrivateObject privSub = new PrivateObject(test, new PrivateType(typeof(SubClass)));
            PrivateObject privBase = new PrivateObject(test, new PrivateType(typeof(BaseClass)));
    
            Assert.AreNotEqual<int>(0, (int)privBase.GetFieldOrProperty("one"));
            Assert.AreNotEqual<int>(0, (int)privSub.GetFieldOrProperty("two"));
        }
    }
    
    0 讨论(0)
  • 2020-12-08 08:21

    Use PrivateType to specify the desired type and use a different constructor of PrivateObject:

    var test = new SubClass();
    var privateType = new PrivateType(typeof(BaseClass));
    var privateObject = new PrivateObject(test, privateType);
    // privateObject.GetFieldOrProperty("one", flags)
    
    0 讨论(0)
  • 2020-12-08 08:23

    As André Pena wrote. Why would you like to test private members of the Baseclass through the Subclass. You wouldn't have access to these members in normal code of your Subclass either. You have to make the Properties members protected to have access to them from the Subclass.

    Then you can also test these Members with the PrivateObject.

    0 讨论(0)
  • 2020-12-08 08:25

    I wanted to do same thing and I made this extension methods. Now it works well. My initial idea is from your post. Thank you!

    https://github.com/cactuaroid/PrivateObjectExtensions

    What it's doing basically is finding member owner by Type.GetFields() and Type.GetProperties() recursively, and then create PrivateObject (or PrivateType) as correct type to access the member.

    0 讨论(0)
  • 2020-12-08 08:33

    PrivateObject is a bit on the "dumb" side when it comes to handling inheritance. Unfortunately, its methods are not virtual, so there's no easy way to change this behaviour. You essentially have two options: either live with the limitations or create your own private accessor helper that can handle inherited members transparently.

    0 讨论(0)
提交回复
热议问题