c# - NUnit 单元测试有 "ExpectedException"但仍然因异常而失败

标签 c# unit-testing exception nunit

我有一个单元测试失败,因为 System.ArgumentException 被抛出,尽管我期待它并且它是故意的行为 - 我错过了什么?

[Test]
[ExpectedException(typeof(ArgumentException), ExpectedMessage = "Seconds from midnight cannot be more than 86400 in 010100712386401000000012")]
public void TestParsingCustomReferenceWithInValidSecondsFromMidnight()
{
    // I am expecting this method to throw an ArgumentException:
    CustomReference.Parse("010100712386401000000012");
}

我也尝试过不设置 ExpectedMessage - 没有区别。

最佳答案

你试过断言语法吗?

Assert.Throws<ArgumentException>(
    () => CustomReference.Parse("010100712386401000000012"),
    "Seconds from midnight cannot be more than 86400 in 010100712386401000000012"
);

关于c# - NUnit 单元测试有 "ExpectedException"但仍然因异常而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1928079/

相关文章:

c# - 如何通过带有密码的 neo4jclient 返回关系及其属性

c# - WPF Dispatcher.Run 在 Win 7 64 上抛出空异常

c# - 在 C# 中的 DateTimePicker 中设置 Null 值

unit-testing - 如何使用 Go 的 moq 模拟 http.Handler?

c# - 异常 :catch(Exception ex) question

c# - 在 C# 中使用 C++ 方法

unit-testing - Scala 中的 JUnit 测试类别?

ruby-on-rails - Rails 4 测试根本没有运行

c++ - 如何通过包含要打印的字符串的类函数打印异常?

c# - mscorlib.dll 中发生奇怪的错误 'System.ExecutionEngineException'