← Back to team overview

nunit-core team mailing list archive

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

 

You have been subscribed to a public bug:

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!

** Affects: nunit-3.0
     Importance: Wishlist
     Assignee: Charlie Poole (charlie.poole)
         Status: Triaged


** Tags: feature
-- 
Add Assert.Throws<TEx>( Func<object> ) for properties testing
https://bugs.launchpad.net/bugs/958770
You received this bug notification because you are a member of NUnit Core Developers, which is the registrant for NUnit Framework.


References