我有一个设置模拟的问题,所以我可以在我的Mocked对象上调用Marshal.ReleaseComObject()
。Mocking and 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);
}
}
}
它工作时,我用真实的一个featurecursor和功能,但是当我嘲笑功能的单元测试,我得到一个错误:
"System.ArgumentException : The object's type must be __ComObject or derived from __ComObject."
但是我怎么申请这个我莫克目的?
漂亮!!非常感谢你。我为什么不考虑制作MarshalWrapper?我在我的代码中包含了其他所有内容:-) – Morten