NUnit的单元测试有"&的ExpectedException QUOT;但仍然未能就例外 [英] NUnit Unit Test has "ExpectedException" but still failing on exception

查看:412
本文介绍了NUnit的单元测试有"&的ExpectedException QUOT;但仍然未能就例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有失败单元测试,因为 System.ArgumentException 被抛出,即使我的希望它和它的故意行为 - 你有什么我错过

I have a unit test that is failing because a System.ArgumentException is being thrown, even though I am expecting it and it's deliberate behaviour - what have I missed?

[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 被设置 - ?没有什么区别

I've also tried without the ExpectedMessage being set - no difference.

推荐答案

你试过断言语法

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

这篇关于NUnit的单元测试有&QUOT;&的ExpectedException QUOT;但仍然未能就例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆