如何对终结器进行单元测试?
我有以下类,它是 IDisposable
对象的装饰器(我省略了它添加的内容),它本身使用通用模式实现了 IDisposable
:
public class DisposableDecorator : IDisposable
{
private readonly IDisposable _innerDisposable;
public DisposableDecorator(IDisposable innerDisposable)
{
_innerDisposable = innerDisposable;
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
~DisposableDecorator()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
_innerDisposable.Dispose();
}
}
我可以轻松测试innerDisposable
在调用 Dispose()
时被释放:
[Test]
public void Dispose__DisposesInnerDisposable()
{
var mockInnerDisposable = new Mock<IDisposable>();
new DisposableDecorator(mockInnerDisposable.Object).Dispose();
mockInnerDisposable.Verify(x => x.Dispose());
}
但是我如何编写一个测试来确保 innerDisposable
不不 被终结器处理掉?我想写这样的东西,但它失败了,大概是因为 GC 线程尚未调用终结器:
[Test]
public void Finalizer__DoesNotDisposeInnerDisposable()
{
var mockInnerDisposable = new Mock<IDisposable>();
new DisposableDecorator(mockInnerDisposable.Object);
GC.Collect();
mockInnerDisposable.Verify(x => x.Dispose(), Times.Never());
}
I have the following class which is a decorator for an IDisposable
object (I have omitted the stuff it adds) which itself implements IDisposable
using a common pattern:
public class DisposableDecorator : IDisposable
{
private readonly IDisposable _innerDisposable;
public DisposableDecorator(IDisposable innerDisposable)
{
_innerDisposable = innerDisposable;
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
~DisposableDecorator()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
_innerDisposable.Dispose();
}
}
I can easily test that innerDisposable
is disposed when Dispose()
is called:
[Test]
public void Dispose__DisposesInnerDisposable()
{
var mockInnerDisposable = new Mock<IDisposable>();
new DisposableDecorator(mockInnerDisposable.Object).Dispose();
mockInnerDisposable.Verify(x => x.Dispose());
}
But how do I write a test to make sure innerDisposable
does not get disposed by the finalizer? I want to write something like this but it fails, presumably because the finalizer hasn't been called by the GC thread:
[Test]
public void Finalizer__DoesNotDisposeInnerDisposable()
{
var mockInnerDisposable = new Mock<IDisposable>();
new DisposableDecorator(mockInnerDisposable.Object);
GC.Collect();
mockInnerDisposable.Verify(x => x.Dispose(), Times.Never());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可能会误解,但是:
可能会成功 - http:// /msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx
I might be misunderstanding, but:
Might do the trick - http://msdn.microsoft.com/en-us/library/system.gc.waitforpendingfinalizers.aspx
在编写单元测试时,您应该始终尝试测试外部可见行为,而不是实现细节。有人可能会说抑制终结确实是不可见的行为,但另一方面,您可能无法(也不应该)模拟垃圾收集器。
在您的情况下,您试图确保遵循“最佳实践”或编码实践。应通过为此目的而制作的工具来强制执行,例如
When writing unit tests, you should always try to test outside visible behavior, not implementation details. One could argue that supressing finalization is indeed outside visible behavior, but on the other hand, there's probably no way you can (nor should you) mock out the garabage collector.
What you try to make sure in your case, is that a "best-practice" or a coding practice is followed. It should be enforced via a tool that is made for this purpose, such as FxCop.
我使用 Appdomain(参见下面的示例)。
TemporaryFile 类在构造函数中创建临时文件,并在 Dispose 或终结器 ~TemporaryFile() 中删除它。
不幸的是,GC.WaitForPendingFinalizers();不能帮助我测试终结器。
I use Appdomain (see sample below).
Class TemporaryFile creates temporary file in constructor and delete's it in Dispose or in finalizer ~TemporaryFile().
Unfortunately, GC.WaitForPendingFinalizers(); doesn't help me to test finalizer.
测试终结并不容易,但测试对象是否是垃圾收集的对象可能会更容易。
这可以通过弱引用来完成。
在测试中,在调用 GC.Collect() 之前让局部变量超出范围非常重要。最简单的确定方法是函数作用域。
It's not easy to test finalization, but it can be easier to test if an object is a subject to garbage collection.
This can be done with a weak references.
In a test, it's important to for the local variables to run out of scope before calling GC.Collect(). The easiest way to make sure is a function scope.
我在我的单元测试项目中找到了一种使用反射来做到这一点的方法:
I found a way to do it in my Unit Test project using Reflection: