PrivateObject 找不到属性

发布于 2024-10-15 19:35:08 字数 874 浏览 5 评论 0原文

我的结构基本上如下所示:

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

   private void DoSomething()
   {

       // ...

       DoSomethingSpecific();
   }

   protected abstract void DoSomethingSpecific();
}

由于复杂性,我需要对 DoSomething 方法进行单元测试,以确保它始终以相同的方式工作。这就是我创建以下存根的原因。

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

我使用 PrivateObject 类来实例化类 AStub 来访问类 A 的方法和属性。这工作了一段时间,由于某种原因,现在每当我尝试访问该属性或方法时就会崩溃。

以下代码用于测试:

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

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

// assert...

抛出的异常是 MissingMethodException 告诉我找不到属性或方法。但是当我调试和检查层次结构时,一切似乎都是正确的,包括拼写。

感谢您的帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

太阳公公是暖光 2024-10-22 19:35:08

您需要将 PrivateType 参数设置为基类才能访问该级别的私有成员。

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

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)));
格子衫的從容 2024-10-22 19:35:08

那不应该是“public class AStub : A”吗?

要解决缺少方法的异常,只需再次编译所有内容(!)。要么你得到一些编译器错误告诉你出了什么问题,要么错误就会消失。

如果仍然不起作用,请检查您是否没有程序集的多个副本(包括 GAC!),并在 Deboug-Out-Window 中观察是否从正确的路径加载程序集。

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.

幼儿园老大 2024-10-22 19:35:08

我刚刚尝试了类似的方法,我认为这是因为该财产受到保护而不是私有。

我在测试程序集中创建了自己的访问器

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
    }
}

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
    }
}
尹雨沫 2024-10-22 19:35:08
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);
   ...
}
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);
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文