PrivateObject 找不到属性
我的结构基本上如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要将 PrivateType 参数设置为基类才能访问该级别的私有成员。
You need to set the PrivateType argument to your base class to access the private members at that level.
那不应该是“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.
我刚刚尝试了类似的方法,我认为这是因为该财产受到保护而不是私有。
我在测试程序集中创建了自己的访问器
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