Add Assert.Throws<TEx>( Func<object> ) for properties testing
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
NUnit Framework |
Triaged
|
Wishlist
|
Charlie Poole |
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 InvalidOperatio
To test for such behavior, one would write
var obj = CreateDisposedO
#pragma warning disable 168
// Assuming Name cannot be accessed after disposing
Assert.
#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<
{
return Assert.Throws<T>(() => { var v = code(); });
}
As a result, our code can now be written as:
var obj = CreateDisposedO
Assert.
Much more concise and self evident.
Thank you!
summary: |
- Add Assert.Throws( Func<T> ) for properties + Add Assert.Throws<TEx>( Func<object> ) for properties testing |
description: | updated |
description: | updated |
Changed in nunitv2: | |
status: | New → Triaged |
importance: | Undecided → Wishlist |
Changed in nunitv2: | |
assignee: | nobody → Charlie Poole (charlie.poole) |
affects: | nunitv2 → nunit-3.0 |
tags: | added: framework |
I would prefer something like:
public static TException Throws<TException, T>(Func<T> action) Throws< TException> (() => { var v = action(); });
where TException : Exception
{
return Assert.
}