[英]How to write a unit-test for the outcome of Dispose()
Is there a good way of testing the result of the IDisposable.Dispose()-method?是否有测试 IDisposable.Dispose() 方法结果的好方法?
Thanks for all your answers.感谢您的所有回答。
To unit test the Dispose
method, invoke Dispose
and then check your mocked versions of your connection, session, and cache.要对
Dispose
方法进行单元测试,请调用Dispose
,然后检查连接、会话和缓存的模拟版本。 Ensure that they are properly closed and cleared.确保它们已正确关闭和清理。
Ensure that after calling the .Dispose()
method that all function and property getters/setters return an ObjectDisposedException
.确保在调用
.Dispose()
方法后,所有函数和属性 getter/setter 都返回ObjectDisposedException
。
http://msdn.microsoft.com/en-us/library/system.objectdisposedexception.aspx http://msdn.microsoft.com/en-us/library/system.objectdisposedexception.aspx
There are 2 sides to this question:
1:If there are any mocked objects which in the actual method are getting disposed
//In actual class properties be like
public Type SomeObj = new Type();
-----------------
//and in dispose() you are disposing it
public void Dispose()
{
SomeObj.Dispose();
}
now in test case you can do as follows:
[Fact]
public void Sometestmethod()
{
var myTestingClass= new TestingClass();
myTestingClass.Dispose();
Assert.Equal(null, myTestingClass.SomeObj);
}
2: If the obj getting disposed is a private obj
private SomeType SomeObj;
public TestingClassCtor(SomeType _someType)
{
SomeObj= _someObj;
}
--------
//in dispose method
public void Dispose()
{
SomeObj.Dispose();
}
Then to write Ut for this scenario
[Fact]
public void SomeTestMethod()
{
//This verifies that Dispose method in real class gets executed without any error.
myTestClass.Dispose();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.