开发者

How to write a unit-test for the outcome of Dispose()

Is there a good way of testing the result开发者_Python百科 of the IDisposable.Dispose()-method?

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. 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.

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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜