C# 捕获异常而不抛出类型
我是xUnit的新手,我正在努力增加我的测试覆盖率。我的类结构如下所示:C# 捕获异常而不抛出类型,c#,exception,xunit,C#,Exception,Xunit,我是xUnit的新手,我正在努力增加我的测试覆盖率。我的类结构如下所示: public class Class1 { public override void Method1() { try { // some code logic } catch (CoreException ex) { BmpException lex = new BmpException();
public class Class1
{
public override void Method1()
{
try
{
// some code logic
}
catch (CoreException ex)
{
BmpException lex = new BmpException();
throw lex;
}
catch (CodedException ex)
{
BmpException lex = new BmpException();
throw lex;
}
}
}
[Fact]
public void TestMethod()
{
Class1 SomeClass = new Class1();
// Works but doesn't affect code coverage ?
Assert.Throws<BmpException>(() => SomeClass.Method1());
// Test failed - Expected: CodedException Actual: BmpException
// Assert.Throws<CodedException>(() => SomeClass.Method1());
// Test failed - Expected: CoreException Actual: BmpException
// Assert.Throws<CoreException>(() => SomeClass.Method1());
}
我的xUnit测试方法如下所示:
public class Class1
{
public override void Method1()
{
try
{
// some code logic
}
catch (CoreException ex)
{
BmpException lex = new BmpException();
throw lex;
}
catch (CodedException ex)
{
BmpException lex = new BmpException();
throw lex;
}
}
}
[Fact]
public void TestMethod()
{
Class1 SomeClass = new Class1();
// Works but doesn't affect code coverage ?
Assert.Throws<BmpException>(() => SomeClass.Method1());
// Test failed - Expected: CodedException Actual: BmpException
// Assert.Throws<CodedException>(() => SomeClass.Method1());
// Test failed - Expected: CoreException Actual: BmpException
// Assert.Throws<CoreException>(() => SomeClass.Method1());
}
[事实]
公共void TestMethod()
{
Class1 SomeClass=新的Class1();
//有效但不影响代码覆盖率?
抛出(()=>SomeClass.Method1());
//测试失败-预期:CodedException实际:BmpException
//抛出(()=>SomeClass.Method1());
//测试失败-预期:CoreException实际:BmpException
//抛出(()=>SomeClass.Method1());
}
因此,我在这里面临的问题是,我需要涵盖CoreException和CodedException块的测试。由于Assert.Throws捕获表达式并断言类型,因此我无法找到解决方法来完成测试覆盖
任何帮助都将不胜感激。谢谢,您可以像这样获得引发的异常:
var ex = Assert.Throws<BmpException>(() => SomeClass.Method1());
Assert.Equal("test message", ex.Message);
您可以按如下方式获取引发的异常:
var ex = Assert.Throws<BmpException>(() => SomeClass.Method1());
Assert.Equal("test message", ex.Message);
我的类结构如下所示:
public class Class1
{
public override void Method1()
{
try
{
// some code logic
}
catch (CoreException ex)
{
BmpException lex = new BmpException();
throw lex;
}
catch (CodedException ex)
{
BmpException lex = new BmpException();
throw lex;
}
}
}
[Fact]
public void TestMethod()
{
Class1 SomeClass = new Class1();
// Works but doesn't affect code coverage ?
Assert.Throws<BmpException>(() => SomeClass.Method1());
// Test failed - Expected: CodedException Actual: BmpException
// Assert.Throws<CodedException>(() => SomeClass.Method1());
// Test failed - Expected: CoreException Actual: BmpException
// Assert.Throws<CoreException>(() => SomeClass.Method1());
}
由于丢失了所有相关信息,因此简单地抛出不同的异常通常是非常糟糕的做法。至少你应该这样做
因为您没有包含所有相关代码
// some code logic
不可能给你一个有计划的答案。基于信息的缺乏,我们可以给出的唯一真实答案是,您的测试需要使某些代码逻辑抛出每种类型的异常(CoreException
和CodedException
),以增加代码覆盖率
//测试失败-预期:CodedException实际:BmpException
//抛出(()=>SomeClass.Method1())
您永远不能将类型断言为CoreException
或CodedException
,因为它们正在被调用。这些异常在方法调用中始终丢失
由于Assert.Throws捕获表达式并断言类型,因此我无法找到解决方法
您必须捕获正在抛出的类型:
var ex = Assert.Throws<BmpException>(() => SomeClass.Method1());
这具有3的圈复杂度,因为有三种可能的路径,即使只有一种结果。要使此代码具有100%的代码覆盖率,必须通过确保逻辑支持结果来测试以下测试:
public class Class1_Tests
{
public AnyBodyWantAPeanut_WithValue1_ReturnsException
{
//Assign
var a = new Class1();
//Act
var method = () => a.AnyBodyWantAPeanut(1);
//Assert
Assert.Throws<Exception>(method);
}
public AnyBodyWantAPeanut_WithValue2_ReturnsException
{
//Assign
var a = new Class1();
//Act
var method = () => a.AnyBodyWantAPeanut(2);
//Assert
Assert.Throws<Exception>(method);
}
public AnyBodyWantAPeanut_WithValue1_ReturnsException
{
//Assign
var a = new Class1();
//Act
var method = () => a.AnyBodyWantAPeanut(0);
//Assert
Assert.Throws<Exception>(method);
}
}
公共类1\u测试
{
public Anybody想要使用Value1\u Returns例外
{
//分配
var a=新的Class1();
//表演
var方法=()=>a.AnyBodyWantAPeanut(1);
//断言
Assert.Throws(方法);
}
任何公共实体都希望获得带值的2\u返回异常
{
//分配
var a=新的Class1();
//表演
var方法=()=>a.AnyBodyWantAPeanut(2);
//断言
Assert.Throws(方法);
}
public Anybody想要使用Value1\u Returns例外
{
//分配
var a=新的Class1();
//表演
var方法=()=>a.AnyBodyWantAPeanut(0);
//断言
Assert.Throws(方法);
}
}
我的类结构如下所示:
public class Class1
{
public override void Method1()
{
try
{
// some code logic
}
catch (CoreException ex)
{
BmpException lex = new BmpException();
throw lex;
}
catch (CodedException ex)
{
BmpException lex = new BmpException();
throw lex;
}
}
}
[Fact]
public void TestMethod()
{
Class1 SomeClass = new Class1();
// Works but doesn't affect code coverage ?
Assert.Throws<BmpException>(() => SomeClass.Method1());
// Test failed - Expected: CodedException Actual: BmpException
// Assert.Throws<CodedException>(() => SomeClass.Method1());
// Test failed - Expected: CoreException Actual: BmpException
// Assert.Throws<CoreException>(() => SomeClass.Method1());
}
由于丢失了所有相关信息,因此简单地抛出不同的异常通常是非常糟糕的做法。至少你应该这样做
因为您没有包含所有相关代码
// some code logic
不可能给你一个有计划的答案。基于信息的缺乏,我们可以给出的唯一真实答案是,您的测试需要使某些代码逻辑抛出每种类型的异常(CoreException
和CodedException
),以增加代码覆盖率
//测试失败-预期:CodedException实际:BmpException
//抛出(()=>SomeClass.Method1())
您永远不能将类型断言为CoreException
或CodedException
,因为它们正在被调用。这些异常在方法调用中始终丢失
由于Assert.Throws捕获表达式并断言类型,因此我无法找到解决方法
您必须捕获正在抛出的类型:
var ex = Assert.Throws<BmpException>(() => SomeClass.Method1());
这具有3的圈复杂度,因为有三种可能的路径,即使只有一种结果。要使此代码具有100%的代码覆盖率,必须通过确保逻辑支持结果来测试以下测试:
public class Class1_Tests
{
public AnyBodyWantAPeanut_WithValue1_ReturnsException
{
//Assign
var a = new Class1();
//Act
var method = () => a.AnyBodyWantAPeanut(1);
//Assert
Assert.Throws<Exception>(method);
}
public AnyBodyWantAPeanut_WithValue2_ReturnsException
{
//Assign
var a = new Class1();
//Act
var method = () => a.AnyBodyWantAPeanut(2);
//Assert
Assert.Throws<Exception>(method);
}
public AnyBodyWantAPeanut_WithValue1_ReturnsException
{
//Assign
var a = new Class1();
//Act
var method = () => a.AnyBodyWantAPeanut(0);
//Assert
Assert.Throws<Exception>(method);
}
}
公共类1\u测试
{
public Anybody想要使用Value1\u Returns例外
{
//分配
var a=新的Class1();
//表演
var方法=()=>a.AnyBodyWantAPeanut(1);
//断言
Assert.Throws(方法);
}
任何公共实体都希望获得带值的2\u返回异常
{
//分配
var a=新的Class1();
//表演
var方法=()=>a.AnyBodyWantAPeanut(2);
//断言
Assert.Throws(方法);
}
public Anybody想要使用Value1\u Returns例外
{
//分配
var a=新的Class1();
//表演
var方法=()=>a.AnyBodyWantAPeanut(0);
//断言
Assert.Throws(方法);
}
}
您的代码无法编译,因为您使用相同的ex
名称作为异常,在相同的范围内声明您是对的,我只是从内存中编写,我只是编辑了我的代码。不过,我的问题还是一样,谢谢throw ex
重置堆栈跟踪,这不是一个好主意。你在每个catch块上抛出BmpException
,为什么你期望它能起作用?那么你有解决我的问题的方法吗?或者您只是在注释?您的异常类是否接受InnerException作为构造函数参数?如果不是的话,它应该这样做。在自定义异常中传递原始异常BmpException lex=new BmpException(ex)代码>这将在InnerException中保留堆栈跟踪。这是包装异常以添加附加信息并保留原始异常的标准方法。请参阅我的更新答案。您的代码无法编译,因为您使用的异常名称与您所使用的范围相同