测试异常并捕获异常细节
Testing for exceptions and catch exception details
我看过其他 post 有关单元测试的内容,但 none 我看到的实际上是测试抛出时异常中的内容。主要目标是通过向助手 class.
发送错误参数来引发异常并检查堆栈跟踪的详细信息
由于原始代码没有抛出异常,我决定对 NUnit 测试进行一些在线研究,并发现了一段非常好的代码,它比我写的代码短很多,但未能检查错误对象。我需要能够 Assert
在堆栈跟踪中显示某些措辞。
最初是代码的样子,但我必须承认它不是很漂亮:
[Test]
public void TestExceptionHandling()
{
try
{
DoExceptionScenario(new SomeCustomRadioButtonControl(), FieldManager.GetField("access_mode"));
}
catch (Exception ex)
{
Assert.IsInstanceOf(typeof(CustomException), ex);
string details = Util.GetExceptionDetails((CustomException)ex);
Assert.IsTrue(details.Contains("Detail Name=\"ControlName\" Description=\"SomeCustomRadioButtonControl\""));
}
}
如您所见,问题可能是一堆误报。
我修改测试的另一种方式是这样的:
[Test]
public void TestExceptionHandling()
{
Assert.That(() => DoExceptionScenario(new SomeCustomRadioButtonControl(), FieldManager.GetField("access_mode")),
Throws.TypeOf<CustomException>());
}
如果没有异常,这将失败。但是,如果有异常,我该如何捕获它并检查它的内容呢?符合以下内容(if
语句在这种情况下有效):
[Test]
public void ShouldControlNameBeListedInStackTrace()
{
bool exceptionStatus = Assert.That(() => DoExceptionScenario(new SomeCustomRadioButtonControl(), FieldManager.GetField("access_mode")),
Throws.TypeOf<CustomException>());
if (exceptionStatus != true)
{
string details = Util.GetExceptionDetails((CustomException)ex);
Assert.IsTrue(details.Contains("detail name=\"controlname\" description=\"SomeCustomRadioButtonControl\""));
}
}
假设 CustomException
class 看起来像这样。它没有做任何事情...只是从基础 Exception
class:
覆盖 "Message" 属性
public class CustomException : Exception
{
private string message;
public override string Message
{
get { return string.Format("{0}: {1}", DateTime.Now, message); }
}
public CustomException(string message)
{
this.message = message;
}
}
并假设您有一个抛出异常的方法,例如:
public class ProductionClass
{
public void SomeMethod()
{
throw new CustomException("Oh noz!");
}
}
这里有一些您可以在 nUnit 中使用的示例测试。你想要最后一个。
[TestFixture]
public class MyTests
{
private ProductionClass p;
[SetUp]
public void Setup()
{
p = new ProductionClass();
}
// Use the ExpectedException attribute to make sure it threw.
[Test]
[ExpectedException(typeof(CustomException)]
public void Test1()
{
p.SomeMethod();
}
// Set the ExpectedMessage param to test for a specific message.
[Test]
[ExpectedException(typeof(CustomException), ExpectedMessage = "Oh nozzzz!")]
public void Test2()
{
p.SomeMethod();
}
// For even more detail, like inspecting the Stack Trace, use Assert.Throws<T>.
[Test]
public void Test3()
{
var ex = Assert.Throws<CustomException>(() => p.SomeMethod());
Assert.IsTrue(ex.StackTrace.Contains("Some expected text"));
}
}
Assert.Throws<T>
方法适用于任何 Exception
。它执行括号中的委托并检测它是否抛出异常。
在上面的测试中,如果它 确实 抛出,它也会测试指定内容的堆栈跟踪。如果两个步骤都通过,则测试通过。
我看过其他 post 有关单元测试的内容,但 none 我看到的实际上是测试抛出时异常中的内容。主要目标是通过向助手 class.
发送错误参数来引发异常并检查堆栈跟踪的详细信息由于原始代码没有抛出异常,我决定对 NUnit 测试进行一些在线研究,并发现了一段非常好的代码,它比我写的代码短很多,但未能检查错误对象。我需要能够 Assert
在堆栈跟踪中显示某些措辞。
最初是代码的样子,但我必须承认它不是很漂亮:
[Test]
public void TestExceptionHandling()
{
try
{
DoExceptionScenario(new SomeCustomRadioButtonControl(), FieldManager.GetField("access_mode"));
}
catch (Exception ex)
{
Assert.IsInstanceOf(typeof(CustomException), ex);
string details = Util.GetExceptionDetails((CustomException)ex);
Assert.IsTrue(details.Contains("Detail Name=\"ControlName\" Description=\"SomeCustomRadioButtonControl\""));
}
}
如您所见,问题可能是一堆误报。
我修改测试的另一种方式是这样的:
[Test]
public void TestExceptionHandling()
{
Assert.That(() => DoExceptionScenario(new SomeCustomRadioButtonControl(), FieldManager.GetField("access_mode")),
Throws.TypeOf<CustomException>());
}
如果没有异常,这将失败。但是,如果有异常,我该如何捕获它并检查它的内容呢?符合以下内容(if
语句在这种情况下有效):
[Test]
public void ShouldControlNameBeListedInStackTrace()
{
bool exceptionStatus = Assert.That(() => DoExceptionScenario(new SomeCustomRadioButtonControl(), FieldManager.GetField("access_mode")),
Throws.TypeOf<CustomException>());
if (exceptionStatus != true)
{
string details = Util.GetExceptionDetails((CustomException)ex);
Assert.IsTrue(details.Contains("detail name=\"controlname\" description=\"SomeCustomRadioButtonControl\""));
}
}
假设 CustomException
class 看起来像这样。它没有做任何事情...只是从基础 Exception
class:
public class CustomException : Exception
{
private string message;
public override string Message
{
get { return string.Format("{0}: {1}", DateTime.Now, message); }
}
public CustomException(string message)
{
this.message = message;
}
}
并假设您有一个抛出异常的方法,例如:
public class ProductionClass
{
public void SomeMethod()
{
throw new CustomException("Oh noz!");
}
}
这里有一些您可以在 nUnit 中使用的示例测试。你想要最后一个。
[TestFixture]
public class MyTests
{
private ProductionClass p;
[SetUp]
public void Setup()
{
p = new ProductionClass();
}
// Use the ExpectedException attribute to make sure it threw.
[Test]
[ExpectedException(typeof(CustomException)]
public void Test1()
{
p.SomeMethod();
}
// Set the ExpectedMessage param to test for a specific message.
[Test]
[ExpectedException(typeof(CustomException), ExpectedMessage = "Oh nozzzz!")]
public void Test2()
{
p.SomeMethod();
}
// For even more detail, like inspecting the Stack Trace, use Assert.Throws<T>.
[Test]
public void Test3()
{
var ex = Assert.Throws<CustomException>(() => p.SomeMethod());
Assert.IsTrue(ex.StackTrace.Contains("Some expected text"));
}
}
Assert.Throws<T>
方法适用于任何 Exception
。它执行括号中的委托并检测它是否抛出异常。
在上面的测试中,如果它 确实 抛出,它也会测试指定内容的堆栈跟踪。如果两个步骤都通过,则测试通过。