PrivateObject does not find property

北城以北 提交于 2019-12-07 08:43:18

问题


I have a structure which looks basicly like this:

abstract class A
{
   protected string Identificator { get; set; }

   private void DoSomething()
   {

       // ...

       DoSomethingSpecific();
   }

   protected abstract void DoSomethingSpecific();
}

Because of the complexity I need do unit tests the DoSomething method to be sure it works allways in the same way. Thats why I created following stub.

public class AStub : A
{
    protected override void DoSomethingSpecific()
    {
        // nothing to do
    }
}

I use the PrivateObject class to access the methods and properties of class A be instantiating class AStub. This worked for a while and for some reason crashes now whenever I try to access either the property or the method.

following code for testing:

var sut = new CommonIodAdapterImpl();
var accessor = new PrivateObject(sut);

accessor.SetProperty("Identificator", "blablub");
accessor.Invoke("DoSomething", null);

// assert...

The exception which is thrown is a MissingMethodException telling me that the propertie or method was not found. But when I debug and check the hierachy every seems to be right inclduing the spelling.

Thank you for your help.


回答1:


You need to set the PrivateType argument to your base class to access the private members at that level.

var accessor = new PrivateObject(sut, new PrivateType(typeof(A)));



回答2:


Shouldn't that be "public class AStub : A"?

To resolve the missing method exception just compile everything(!) once more. Either you get some compiler error telling you what's wrong or the error will vanish.

If it still doesn't work, check if you don't have multiple copies of the assemblies (including GAC!) and watch in the Deboug-Out-Window if it loads the assemblies from the correct path.




回答3:


I just tried something similar, i assmued it's because the property is protected rather than private.

I created my own accessor in my test assembly

public class AAccessor : A
{
    // use this instead of Identificator
    public string IdentificatorAccessor 
    {
        get { return this.Identificator; }
        set { this.Identificator = value; }
    }

    // test this method in your unit test
    public void DoSomethingAccessor()
    {
        this.DoSomethingSpecific()
    }

    // need this to satisfy the abstract class
    protected override void DoSomethingSpecific()
    {
        // do nothing here
    }
}



回答4:


public class BaseClass
{
   private int _fieldToSet;
   ...
}

public class DerivedClass : BaseClass
{
   ...
}

// Unit Test Code

public void Test()
{
   DerivedClass d = new DerivedClass();
   PrivateObject privObj = new PrivateObject(d, new PrivateType(typeof(BaseClass));
   privObj.SetFieldOrProperty("fieldToSet", 8675309);
   ...
}


来源:https://stackoverflow.com/questions/4883944/privateobject-does-not-find-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!