【发布时间】:2014-02-12 12:07:18
【问题描述】:
我在设置模拟时遇到问题,所以我可以在我的模拟对象上调用 Marshal.ReleaseComObject()。
我正在使用 Moq 设置 IFeature 类型的模拟(来自第三方接口库)。模拟设置相当简单:
var featureMock = new Mock<IFeature>();
IFeature feature = featureMock.Object;
在我的代码中,功能对象是在一个 while 循环中创建的,通过一种游标 (FeatureCursor) 运行。由于第三方库的遗留问题,Feature 对象存在内存泄漏问题。因此,我必须通过Marshal.ReleaseComObject() 释放对象,如代码所示;
public class XXX
{
public void DoThis()
{
IFeatureCursor featureCursor;
//...fill the cursor with features;
IFeature feature = null;
while ((feature = featureCursor.NextFeature)!= null)
{
//Do my stuff with the feature
Marshal.ReleaseComObject(feature);
}
}
}
当我使用真正的特征光标和特征时它可以工作,但是当我在单元测试中模拟该特征时,我得到一个错误:
"System.ArgumentException : The object's type must be __ComObject or derived from __ComObject."
但是如何将它应用到我的 Mock 对象?
【问题讨论】:
标签: c# unit-testing mocking moq