C# ExpectedException将ExceptionType提取到System.Type变量
以下方法有效C# ExpectedException将ExceptionType提取到System.Type变量,c#,unit-testing,exception,C#,Unit Testing,Exception,以下方法有效 [TestMethod] [ExpectedException(typeof(ApiException), StaticStringHelper.ApiDoesNotSupportThisMethod)] public async Task CreditCard_GetAll_MethodNotAllowed() { //Test } StaticStringHelper.apidesnotsupportThisMetho
[TestMethod]
[ExpectedException(typeof(ApiException), StaticStringHelper.ApiDoesNotSupportThisMethod)]
public async Task CreditCard_GetAll_MethodNotAllowed()
{
//Test
}
StaticStringHelper.apidesnotsupportThisMethod
是一个字符串变量。
我想将ExceptionType提取到一个变量中,以便它可以在所有单元测试中重用。原因是,若我为将来不允许的方法引入自定义异常,而不是泛型异常,那个么我只需要更改一个位置
private static Type typeVariable = Type.GetType("ApiException");
[TestMethod]
[ExpectedException(typeVariable, StaticStringHelper.ApiDoesNotSupportThisMethod)]
public async Task CreditCard_GetAll_MethodNotAllowed()
{
}
这是行不通的
根据参数,它接受“System.Type”作为第一个参数
有什么方法可以绕开吗?你是说private const Type=typeof(ApiException)?你不能这么做。你是说private const Type=typeof(ApiException)?你不能这么做