-1

I'm using c# in visual studio 2017.

When performing my unit tests, do i need to call the dispose() method to dispose the connection with the database? Or are opened connections automatically closed?

Thanks in advance

lildeveloper
  • 27
  • 1
  • 2
  • 5
  • You do need to close the SQL connections as soon as possible. See https://stackoverflow.com/questions/61092/close-and-dispose-which-to-call and also https://stackoverflow.com/questions/33116834/is-it-necessary-to-dispose-sqlconnection-as-well-as-sqlcommand – Rui Jarimba Oct 05 '18 at 08:31
  • No, open connections are not automatically closed. – Rui Jarimba Oct 05 '18 at 08:31

1 Answers1

0

This is too general because oyu do not provide a specific example. There are many ways to write a unit test.

if you open a connection WITHIN a unit test, either directly or indirectly (EF) then obviously you close it. If you rely on wrapping start and stop methods and have the connection per test prepared, you obviously do so in the wrapping methods.

It really depends. General rule: Dispose in a test what you create in a test.

TomTom
  • 61,059
  • 10
  • 88
  • 148