有没有一种很好的方法来测试IDisposable.Dispose() - 方法的结果?
感谢您的所有答案。
答案 0 :(得分:2)
要对Dispose
方法进行单元测试,请调用Dispose
,然后检查连接,会话和缓存的模拟版本。确保它们已正确关闭并清除。
答案 1 :(得分:0)
确保在调用.Dispose()
方法后,所有函数和属性getter / setter都返回ObjectDisposedException
。
http://msdn.microsoft.com/en-us/library/system.objectdisposedexception.aspx
答案 2 :(得分:0)
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();
}