我是一个应用程序的单元测试代码,我对如何测试try / catch异常感到困惑。什么是测试此方法的好方法?
public static Exception Create<TException>(string format, params object[] args) where TException : Exception
{
var type = typeof(TException);
try
{
if (type == typeof(ArgumentOutOfRangeException))
{
return (TException)Activator.CreateInstance(type, String.Empty, String.Format(format, args));
}
if (type == typeof(ArgumentNullException))
{
var name = format;
return (TException)Activator.CreateInstance(type, String.Empty, Constants.Strings.ValueCannotBeNull.FormatWith(name));
}
return (TException)Activator.CreateInstance(type, String.Format(format, args));
}
catch (MissingMethodException)
{
try
{
return (TException)Activator.CreateInstance(type);
}
catch (MissingMethodException ex)
{
return new Exception(ParameterlessConstructorMissing.FormatWith(type), ex);
}
}
}