@@ -411,6 +411,8 @@ public void ExecuteScalar_Connection_Null()
411411 Assert . StartsWith ( "ExecuteScalar:" , ex . Message ) ;
412412 }
413413
414+ #region Prepare()
415+
414416 [ Fact ]
415417 public void Prepare_Connection_Null ( )
416418 {
@@ -427,51 +429,92 @@ public void Prepare_Connection_Null()
427429 }
428430
429431 [ Fact ]
430- public void Prepare_Connection_Closed ( )
432+ public void Prepare_ConnectionClosed_TextWithoutParams ( )
431433 {
432- string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
433- + "Password=d" ;
434- SqlConnection cn = new SqlConnection ( connectionString ) ;
435-
436- SqlCommand cmd ;
437-
438- // Text, without parameters
439- cmd = new SqlCommand ( "select count(*) from whatever" , cn ) ;
440- cmd . Prepare ( ) ;
441-
442- // Text, with parameters
443- cmd = new SqlCommand ( "select count(*) from whatever" , cn ) ;
444- cmd . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
445-
446- InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . Prepare ( ) ) ;
447- // Prepare requires an open and available
448- // Connection. The connection's current state
449- // is Closed
450- Assert . Null ( ex . InnerException ) ;
451- Assert . NotNull ( ex . Message ) ;
452- Assert . True ( ex . Message . IndexOf ( "Prepare" , StringComparison . Ordinal ) != - 1 ) ;
434+ // Arrange
435+ using SqlConnection connection = GetNonConnectingConnection ( ) ;
436+ using SqlCommand command = new SqlCommand ( ) ;
437+ command . Connection = connection ;
438+ command . CommandType = CommandType . Text ;
439+ command . CommandText = COMMAND_TEXT ;
440+
441+ // Act / Assert
442+ command . Prepare ( ) ;
443+ Assert . Equal ( ConnectionState . Closed , connection . State ) ;
444+ }
453445
454- // Text, parameters cleared
455- cmd = new SqlCommand ( "select count(*) from whatever" , cn ) ;
456- cmd . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
457- cmd . Parameters . Clear ( ) ;
458- cmd . Prepare ( ) ;
446+ [ Fact ]
447+ public void Prepare_ConnectionClosed_TextWithClearedParams ( )
448+ {
449+ // Arrange
450+ using SqlConnection connection = GetNonConnectingConnection ( ) ;
451+ using SqlCommand command = new SqlCommand ( ) ;
452+ command . Connection = connection ;
453+ command . CommandType = CommandType . Text ;
454+ command . CommandText = COMMAND_TEXT ;
455+ command . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
456+ command . Parameters . Clear ( ) ;
457+
458+ // Act / Assert
459+ command . Prepare ( ) ;
460+ Assert . Equal ( ConnectionState . Closed , connection . State ) ;
461+ }
459462
460- // StoredProcedure, without parameters
461- cmd = new SqlCommand ( "FindCustomer" , cn ) ;
462- cmd . CommandType = CommandType . StoredProcedure ;
463- cmd . Prepare ( ) ;
463+ [ Fact ]
464+ public void Prepare_ConnectionClosed_TextWithParams ( )
465+ {
466+ // Arrange
467+ using SqlConnection connection = GetNonConnectingConnection ( ) ;
468+ using SqlCommand command = new SqlCommand ( ) ;
469+ command . Connection = connection ;
470+ command . CommandType = CommandType . Text ;
471+ command . CommandText = COMMAND_TEXT ;
472+ command . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
473+
474+ // Act
475+ Action action = ( ) => command . Prepare ( ) ;
476+
477+ // Assert
478+ var exception = Assert . Throws < InvalidOperationException > ( action ) ;
479+ Assert . Null ( exception . InnerException ) ;
480+ Assert . NotNull ( exception . Message ) ;
481+ Assert . Contains ( "Prepare" , exception . Message ) ;
482+
483+ Assert . Equal ( ConnectionState . Closed , connection . State ) ;
484+ }
464485
465- // StoredProcedure, with parameters
466- cmd = new SqlCommand ( "FindCustomer" , cn ) ;
467- cmd . CommandType = CommandType . StoredProcedure ;
468- cmd . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
469- cmd . Prepare ( ) ;
486+ [ Fact ]
487+ public void Prepare_ConnectionClosed_SprocWithoutParams ( )
488+ {
489+ // Arrange
490+ using SqlConnection connection = GetNonConnectingConnection ( ) ;
491+ using SqlCommand command = new SqlCommand ( ) ;
492+ command . Connection = connection ;
493+ command . CommandType = CommandType . StoredProcedure ;
494+ command . CommandText = "FindCustomer" ;
495+
496+ // Act / Assert
497+ command . Prepare ( ) ;
498+ Assert . Equal ( ConnectionState . Closed , connection . State ) ;
499+ }
470500
471- // ensure connection was not implictly opened
472- Assert . Equal ( ConnectionState . Closed , cn . State ) ;
501+ [ Fact ]
502+ public void Prepare_ConnectionClosed_SprocWithParams ( )
503+ {
504+ // Arrange
505+ using SqlConnection connection = GetNonConnectingConnection ( ) ;
506+ using SqlCommand command = new SqlCommand ( ) ;
507+ command . Connection = connection ;
508+ command . CommandType = CommandType . StoredProcedure ;
509+ command . CommandText = "FindCustomer" ;
510+
511+ // Act / Assert
512+ command . Prepare ( ) ;
513+ Assert . Equal ( ConnectionState . Closed , connection . State ) ;
473514 }
474515
516+ #endregion
517+
475518 [ Fact ]
476519 public void ResetCommandTimeout ( )
477520 {
@@ -518,5 +561,8 @@ public void ParameterCollectionTest()
518561 cmd . Parameters . Remove ( cmd . Parameters [ 0 ] ) ;
519562 }
520563 }
564+
565+ private static SqlConnection GetNonConnectingConnection ( ) =>
566+ new SqlConnection ( "Initial Catalog=a;Server=b;User ID=c;Password=d" ) ;
521567 }
522568}
0 commit comments