I would do:
public TException Throws<TException>(Action act) where TException : Exception
{
// act
try { act(); } catch (TException ex) { return ex; }
// assert
Assert.Fail("Expected exception");
return default(TException); //never reached
}
Then you can do
Throws<ArgumentNullException>(() => { customer.Name = null; });
Note that NUnit has this method built in (Assert.Throws/Catch
) so you don't need this if you're using that.