Should Dispose methods be unit tested?

后端 未结 4 817
自闭症患者
自闭症患者 2021-01-03 23:25

I am using C#. Is it advised to unit test dispose methods? If so why, and how should one test these methods?

4条回答
  •  盖世英雄少女心
    2021-01-04 00:08

    Big yes - if your situation requires you to implement a Dispose function - you better make sure it does what you think!

    For example, we have classes that coordinate database tasks (think SSIS packages, but with SqlConnection and SqlCommand and SqlBulkCopy etc.).

    If I don't properly implement my Dispose, I could have an uncommitted SqlTransaction, or dangling SqlConnection. This would be VERY bad if I were running multiple instances of these database tasks in series.

提交回复
热议问题