← Back to team overview

nunit-core team mailing list archive

[Bug 958770] Re: Add Assert.Throws<TEx>( Func<object> ) for properties testing

 

** Summary changed:

- Add Assert.Throws( Func<T> ) for properties
+ Add Assert.Throws<TEx>( Func<object> ) for properties testing

** Description changed:

  Sometimes an object is in a certain state that causes reading its
  properties to throw exceptions. For example, the object might be
  disposed, and will throw InvalidOperationException().
  
  To test for such behavior, one would write
  
  var obj = CreateDisposedObject();
  
  #pragma warning disable 168
  Assert.Throws<InvalidOperationException>(() => { var v = obj.Name; }); // Assuming Name cannot be accessed passed disposing
  #pragma warning restore 168
  
+ This is clearly inconvinient and instead can be remedied with this
+ overload (and similar for error message formatting)
  
- This is clearly inconvinient and instead can be remedied with this overload (and similar for error message formatting)
- 
-         public static T Throws<T>(Func<object> code) where T : Exception
-         {
-             return Assert.Throws<T>(() => { var v = code(); });
-         }
+         public static T Throws<T>(Func<object> code) where T : Exception
+         {
+             return Assert.Throws<T>(() => { var v = code(); });
+         }
  
  Having this overload would allow us to write:
  
  var obj = CreateDisposedObject();
  Assert.Throws<InvalidOperationException>(() => obj.Name);
  
  Much more consise and does not require pragmas.

** Description changed:

- Sometimes an object is in a certain state that causes reading its
- properties to throw exceptions. For example, the object might be
- disposed, and will throw InvalidOperationException().
+ Sometimes an object is in a state that causes reading its properties to
+ throw exception. For example, an object might be disposed, and will
+ throw InvalidOperationException().
  
  To test for such behavior, one would write
  
- var obj = CreateDisposedObject();
  
+     var obj = CreateDisposedObject();
  #pragma warning disable 168
- Assert.Throws<InvalidOperationException>(() => { var v = obj.Name; }); // Assuming Name cannot be accessed passed disposing
+     // Assuming Name cannot be accessed after disposing
+     Assert.Throws<InvalidOperationException>(() => { var v = obj.Name; });
  #pragma warning restore 168
  
- This is clearly inconvinient and instead can be remedied with this
- overload (and similar for error message formatting)
  
-         public static T Throws<T>(Func<object> code) where T : Exception
-         {
-             return Assert.Throws<T>(() => { var v = code(); });
-         }
+ Having pragmas and a temp variable is clearly inconvenient, and instead can be remedied with this overload:
  
- Having this overload would allow us to write:
  
- var obj = CreateDisposedObject();
- Assert.Throws<InvalidOperationException>(() => obj.Name);
+     public static T Throws<T>(Func<object> code) where T : Exception
+     {
+         return Assert.Throws<T>(() => { var v = code(); });
+     }
  
- Much more consise and does not require pragmas.
+ As a result, our code can now be written as:
+ 
+     var obj = CreateDisposedObject();
+     Assert.Throws<InvalidOperationException>(() => obj.Name);
+ 
+ 
+ Much more concise and self evident.
+ 
+ Thank you!

-- 
You received this bug notification because you are a member of NUnit
Developers, which is subscribed to NUnit V2.
https://bugs.launchpad.net/bugs/958770

Title:
  Add Assert.Throws<TEx>( Func<object> ) for properties testing

Status in NUnit V2 Test Framework:
  New

Bug description:
  Sometimes an object is in a state that causes reading its properties
  to throw exception. For example, an object might be disposed, and will
  throw InvalidOperationException().

  To test for such behavior, one would write

  
      var obj = CreateDisposedObject();
  #pragma warning disable 168
      // Assuming Name cannot be accessed after disposing
      Assert.Throws<InvalidOperationException>(() => { var v = obj.Name; });
  #pragma warning restore 168

  
  Having pragmas and a temp variable is clearly inconvenient, and instead can be remedied with this overload:

  
      public static T Throws<T>(Func<object> code) where T : Exception
      {
          return Assert.Throws<T>(() => { var v = code(); });
      }

  As a result, our code can now be written as:

      var obj = CreateDisposedObject();
      Assert.Throws<InvalidOperationException>(() => obj.Name);

  
  Much more concise and self evident.

  Thank you!

To manage notifications about this bug go to:
https://bugs.launchpad.net/nunitv2/+bug/958770/+subscriptions


References