@@ -258,7 +258,7 @@ public void Clone()
258
258
}
259
259
260
260
[ Fact ]
261
- public void CommandText ( )
261
+ public void CommandText ( )
262
262
{
263
263
SqlCommand cmd = new SqlCommand ( ) ;
264
264
cmd . CommandText = COMMAND_TEXT ;
@@ -287,37 +287,25 @@ public void CommandTimeout()
287
287
public void CommandTimeout_Value_Negative ( )
288
288
{
289
289
SqlCommand cmd = new SqlCommand ( ) ;
290
- try
291
- {
292
- cmd . CommandTimeout = - 1 ;
293
- }
294
- catch ( ArgumentException ex )
295
- {
296
- // Invalid CommandTimeout value -1; the value must be >= 0
297
- Assert . Equal ( typeof ( ArgumentException ) , ex . GetType ( ) ) ;
298
- Assert . Null ( ex . InnerException ) ;
299
- Assert . NotNull ( ex . Message ) ;
300
- Assert . Equal ( "CommandTimeout" , ex . ParamName ) ;
301
- }
290
+
291
+ ArgumentException ex = Assert . Throws < ArgumentException > ( ( ) => cmd . CommandTimeout = - 1 ) ;
292
+ // Invalid CommandTimeout value -1; the value must be >= 0
293
+ Assert . Null ( ex . InnerException ) ;
294
+ Assert . NotNull ( ex . Message ) ;
295
+ Assert . Equal ( "CommandTimeout" , ex . ParamName ) ;
302
296
}
303
297
304
298
[ Fact ]
305
299
public void CommandType_Value_Invalid ( )
306
300
{
307
301
SqlCommand cmd = new SqlCommand ( ) ;
308
- try
309
- {
310
- cmd . CommandType = ( CommandType ) ( 666 ) ;
311
- }
312
- catch ( ArgumentOutOfRangeException ex )
313
- {
314
- // The CommandType enumeration value, 666, is invalid
315
- Assert . Equal ( typeof ( ArgumentOutOfRangeException ) , ex . GetType ( ) ) ;
316
- Assert . Null ( ex . InnerException ) ;
317
- Assert . NotNull ( ex . Message ) ;
318
- Assert . True ( ex . Message . IndexOf ( "666" ) != - 1 ) ;
319
- Assert . Equal ( "CommandType" , ex . ParamName ) ;
320
- }
302
+
303
+ ArgumentOutOfRangeException ex = Assert . Throws < ArgumentOutOfRangeException > ( ( ) => cmd . CommandType = ( CommandType ) ( 666 ) ) ;
304
+ // The CommandType enumeration value, 666, is invalid
305
+ Assert . Null ( ex . InnerException ) ;
306
+ Assert . NotNull ( ex . Message ) ;
307
+ Assert . True ( ex . Message . IndexOf ( "666" ) != - 1 ) ;
308
+ Assert . Equal ( "CommandType" , ex . ParamName ) ;
321
309
}
322
310
323
311
[ Fact ]
@@ -339,39 +327,27 @@ public void ExecuteNonQuery_Connection_Closed()
339
327
SqlConnection cn = new SqlConnection ( connectionString ) ;
340
328
341
329
SqlCommand cmd = new SqlCommand ( "delete from whatever" , cn ) ;
342
- try
343
- {
344
- cmd . ExecuteNonQuery ( ) ;
345
- }
346
- catch ( InvalidOperationException ex )
347
- {
348
- // ExecuteNonQuery requires an open and available
349
- // Connection. The connection's current state is
350
- // closed.
351
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
352
- Assert . Null ( ex . InnerException ) ;
353
- Assert . NotNull ( ex . Message ) ;
354
- Assert . True ( ex . Message . IndexOf ( "ExecuteNonQuery" ) != - 1 ) ;
355
- }
330
+
331
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . ExecuteNonQuery ( ) ) ;
332
+ // ExecuteNonQuery requires an open and available
333
+ // Connection. The connection's current state is
334
+ // closed.
335
+ Assert . Null ( ex . InnerException ) ;
336
+ Assert . NotNull ( ex . Message ) ;
337
+ Assert . True ( ex . Message . IndexOf ( "ExecuteNonQuery" ) != - 1 ) ;
356
338
}
357
339
358
340
[ Fact ]
359
341
public void ExecuteNonQuery_Connection_Null ( )
360
342
{
361
343
SqlCommand cmd = new SqlCommand ( "delete from whatever" ) ;
362
- try
363
- {
364
- cmd . ExecuteNonQuery ( ) ;
365
- }
366
- catch ( InvalidOperationException ex )
367
- {
368
- // ExecuteNonQuery: Connection property has not
369
- // been initialized
370
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
371
- Assert . Null ( ex . InnerException ) ;
372
- Assert . NotNull ( ex . Message ) ;
373
- Assert . StartsWith ( "ExecuteNonQuery:" , ex . Message ) ;
374
- }
344
+
345
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . ExecuteNonQuery ( ) ) ;
346
+ // ExecuteNonQuery: Connection property has not
347
+ // been initialized
348
+ Assert . Null ( ex . InnerException ) ;
349
+ Assert . NotNull ( ex . Message ) ;
350
+ Assert . StartsWith ( "ExecuteNonQuery:" , ex . Message ) ;
375
351
}
376
352
377
353
[ Fact ]
@@ -382,39 +358,26 @@ public void ExecuteReader_Connection_Closed()
382
358
SqlConnection cn = new SqlConnection ( connectionString ) ;
383
359
384
360
SqlCommand cmd = new SqlCommand ( "Select count(*) from whatever" , cn ) ;
385
- try
386
- {
387
- cmd . ExecuteReader ( ) ;
388
- }
389
- catch ( InvalidOperationException ex )
390
- {
391
- // ExecuteReader requires an open and available
392
- // Connection. The connection's current state is
393
- // closed.
394
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
395
- Assert . Null ( ex . InnerException ) ;
396
- Assert . NotNull ( ex . Message ) ;
397
- Assert . True ( ex . Message . IndexOf ( "ExecuteReader" ) != - 1 ) ;
398
- }
361
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . ExecuteReader ( ) ) ;
362
+ // ExecuteReader requires an open and available
363
+ // Connection. The connection's current state is
364
+ // closed.
365
+ Assert . Null ( ex . InnerException ) ;
366
+ Assert . NotNull ( ex . Message ) ;
367
+ Assert . True ( ex . Message . IndexOf ( "ExecuteReader" ) != - 1 ) ;
399
368
}
400
369
401
370
[ Fact ]
402
371
public void ExecuteReader_Connection_Null ( )
403
372
{
404
373
SqlCommand cmd = new SqlCommand ( "select * from whatever" ) ;
405
- try
406
- {
407
- cmd . ExecuteReader ( ) ;
408
- }
409
- catch ( InvalidOperationException ex )
410
- {
411
- // ExecuteReader: Connection property has not
412
- // been initialized
413
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
414
- Assert . Null ( ex . InnerException ) ;
415
- Assert . NotNull ( ex . Message ) ;
416
- Assert . StartsWith ( "ExecuteReader:" , ex . Message ) ;
417
- }
374
+
375
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . ExecuteReader ( ) ) ;
376
+ // ExecuteReader: Connection property has not
377
+ // been initialized
378
+ Assert . Null ( ex . InnerException ) ;
379
+ Assert . NotNull ( ex . Message ) ;
380
+ Assert . StartsWith ( "ExecuteReader:" , ex . Message ) ;
418
381
}
419
382
420
383
[ Fact ]
@@ -425,39 +388,27 @@ public void ExecuteScalar_Connection_Closed()
425
388
SqlConnection cn = new SqlConnection ( connectionString ) ;
426
389
427
390
SqlCommand cmd = new SqlCommand ( "Select count(*) from whatever" , cn ) ;
428
- try
429
- {
430
- cmd . ExecuteScalar ( ) ;
431
- }
432
- catch ( InvalidOperationException ex )
433
- {
434
- // ExecuteScalar requires an open and available
435
- // Connection. The connection's current state is
436
- // closed.
437
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
438
- Assert . Null ( ex . InnerException ) ;
439
- Assert . NotNull ( ex . Message ) ;
440
- Assert . True ( ex . Message . IndexOf ( "ExecuteScalar" ) != - 1 ) ;
441
- }
391
+
392
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . ExecuteScalar ( ) ) ;
393
+ // ExecuteScalar requires an open and available
394
+ // Connection. The connection's current state is
395
+ // closed.
396
+ Assert . Null ( ex . InnerException ) ;
397
+ Assert . NotNull ( ex . Message ) ;
398
+ Assert . True ( ex . Message . IndexOf ( "ExecuteScalar" ) != - 1 ) ;
442
399
}
443
400
444
401
[ Fact ] // bug #412584
445
402
public void ExecuteScalar_Connection_Null ( )
446
403
{
447
404
SqlCommand cmd = new SqlCommand ( "select count(*) from whatever" ) ;
448
- try
449
- {
450
- cmd . ExecuteScalar ( ) ;
451
- }
452
- catch ( InvalidOperationException ex )
453
- {
454
- // ExecuteScalar: Connection property has not
455
- // been initialized
456
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
457
- Assert . Null ( ex . InnerException ) ;
458
- Assert . NotNull ( ex . Message ) ;
459
- Assert . StartsWith ( "ExecuteScalar:" , ex . Message ) ;
460
- }
405
+
406
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . ExecuteScalar ( ) ) ;
407
+ // ExecuteScalar: Connection property has not
408
+ // been initialized
409
+ Assert . Null ( ex . InnerException ) ;
410
+ Assert . NotNull ( ex . Message ) ;
411
+ Assert . StartsWith ( "ExecuteScalar:" , ex . Message ) ;
461
412
}
462
413
463
414
[ Fact ]
@@ -468,13 +419,11 @@ public void Prepare_Connection_Null()
468
419
// Text, with parameters
469
420
cmd = new SqlCommand ( "select count(*) from whatever" ) ;
470
421
cmd . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
471
- try
472
- {
473
- cmd . Prepare ( ) ;
474
- }
475
- catch ( InvalidOperationException )
476
- {
477
- }
422
+
423
+ InvalidOperationException ex = Assert . Throws < InvalidOperationException > ( ( ) => cmd . Prepare ( ) ) ;
424
+ Assert . Null ( ex . InnerException ) ;
425
+ Assert . NotNull ( ex . Message ) ;
426
+ Assert . StartsWith ( "Prepare: Connection property has not been initialized." , ex . Message ) ;
478
427
}
479
428
480
429
[ Fact ]
@@ -493,20 +442,14 @@ public void Prepare_Connection_Closed()
493
442
// Text, with parameters
494
443
cmd = new SqlCommand ( "select count(*) from whatever" , cn ) ;
495
444
cmd . Parameters . Add ( "@TestPar1" , SqlDbType . Int ) ;
496
- try
497
- {
498
- cmd . Prepare ( ) ;
499
- }
500
- catch ( InvalidOperationException ex )
501
- {
502
- // Prepare requires an open and available
503
- // Connection. The connection's current state
504
- // is Closed
505
- Assert . Equal ( typeof ( InvalidOperationException ) , ex . GetType ( ) ) ;
506
- Assert . Null ( ex . InnerException ) ;
507
- Assert . NotNull ( ex . Message ) ;
508
- Assert . True ( ex . Message . IndexOf ( "Prepare" ) != - 1 ) ;
509
- }
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" ) != - 1 ) ;
510
453
511
454
// Text, parameters cleared
512
455
cmd = new SqlCommand ( "select count(*) from whatever" , cn ) ;
@@ -553,19 +496,13 @@ public void UpdatedRowSource()
553
496
public void UpdatedRowSource_Value_Invalid ( )
554
497
{
555
498
SqlCommand cmd = new SqlCommand ( ) ;
556
- try
557
- {
558
- cmd . UpdatedRowSource = ( UpdateRowSource ) 666 ;
559
- }
560
- catch ( ArgumentOutOfRangeException ex )
561
- {
562
- // The UpdateRowSource enumeration value,666,
563
- // is invalid
564
- Assert . Equal ( typeof ( ArgumentOutOfRangeException ) , ex . GetType ( ) ) ;
565
- Assert . Null ( ex . InnerException ) ;
566
- Assert . NotNull ( ex . Message ) ;
567
- Assert . Equal ( "UpdateRowSource" , ex . ParamName ) ;
568
- }
499
+
500
+ ArgumentOutOfRangeException ex = Assert . Throws < ArgumentOutOfRangeException > ( ( ) => cmd . UpdatedRowSource = ( UpdateRowSource ) 666 ) ;
501
+ // The UpdateRowSource enumeration value,666,
502
+ // is invalid
503
+ Assert . Null ( ex . InnerException ) ;
504
+ Assert . NotNull ( ex . Message ) ;
505
+ Assert . Equal ( "UpdateRowSource" , ex . ParamName ) ;
569
506
}
570
507
571
508
[ Fact ]
@@ -575,8 +512,8 @@ public void ParameterCollectionTest()
575
512
{
576
513
cmd . Parameters . Add ( new SqlParameter ( ) ) ;
577
514
cmd . Parameters . AddRange ( new SqlParameter [ ] { } ) ;
578
- cmd . Parameters . Insert ( 0 , new SqlParameter ( ) ) ;
579
- cmd . Parameters . Insert ( 1 , new SqlParameter ( ) ) ;
515
+ cmd . Parameters . Insert ( 0 , new SqlParameter ( ) ) ;
516
+ cmd . Parameters . Insert ( 1 , new SqlParameter ( ) ) ;
580
517
cmd . Parameters . RemoveAt ( 0 ) ;
581
518
cmd . Parameters . Remove ( cmd . Parameters [ 0 ] ) ;
582
519
}
0 commit comments