我是否需要实现try / catch期望才能测试此方法?不确定从哪里开始?

时间:2019-05-22 14:40:51

标签: c# unit-testing

我是一个应用程序的单元测试代码,我对如何测试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);
          }
      }
  }

0 个答案:

没有答案