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);
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 InvalidOperatio nException( ).
To test for such behavior, one would write
var obj = CreateDisposedO bject() ;
#pragma warning disable 168 Throws< InvalidOperatio nException> (() => { var v = obj.Name; }); // Assuming Name cannot be accessed passed disposing
Assert.
#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 = CreateDisposedO bject() ; Throws< InvalidOperatio nException> (() => obj.Name);
Assert.
Much more consise and does not require pragmas.