2012-03-18 20:05:25 |
Yuri Astrakhan |
bug |
|
|
added bug |
2012-03-18 20:17:40 |
Yuri Astrakhan |
summary |
Add Assert.Throws( Func<T> ) for properties |
Add Assert.Throws<TEx>( Func<object> ) for properties testing |
|
2012-03-18 20:18:12 |
Yuri Astrakhan |
description |
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)
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. |
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)
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. |
|
2012-03-18 20:22:25 |
Yuri Astrakhan |
description |
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)
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. |
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! |
|
2012-07-09 13:35:38 |
Charlie Poole |
nunitv2: status |
New |
Triaged |
|
2012-07-09 13:35:39 |
Charlie Poole |
nunitv2: importance |
Undecided |
Wishlist |
|
2012-07-09 18:57:10 |
Charlie Poole |
nunitv2: assignee |
|
Charlie Poole (charlie.poole) |
|
2012-07-09 19:44:07 |
Charlie Poole |
affects |
nunitv2 |
nunit-3.0 |
|
2013-10-05 05:33:53 |
Charlie Poole |
tags |
feature |
feature framework |
|