NUnit 单元测试有“ExpectedException”但仍然因异常而失败

发布于 2024-08-15 12:50:39 字数 530 浏览 2 评论 0原文

我有一个单元测试失败,因为抛出了 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 - 没有区别。

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");
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所谓喜欢 2024-08-22 12:50:39

您尝试过断言语法吗?

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

Have you tried the assertion syntax?

Assert.Throws<ArgumentException>(
    () => CustomReference.Parse("010100712386401000000012"),
    "Seconds from midnight cannot be more than 86400 in 010100712386401000000012"
);
生生不灭 2024-08-22 12:50:39

预期的消息是否正确?这与 CustomReference.Parse(string) 抛出的消息完全相同吗?例如,它不是 NUnit 控制台中显示的内容。

我不知道这行不通的另一个原因。您使用什么版本的 NUnit?

Is the expected message correct? Is that the exact same message that CustomReference.Parse(string) throws? For example, it is not what is being displayed in the NUnit console.

I wouldn't know another reason why this would not work. What version of NUnit are you using?

孤独患者 2024-08-22 12:50:39

如果你这样做会发生什么?

[TestFixture]
public class CustomReferenceTests
{
    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void TestParsingCustomReferenceWithInValidSecondsFromMidnight()
    {
        // I am expecting this method to throw an ArgumentException:
        CustomReference.Parse("010100712386401000000012");
    }

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

public class CustomReference
{
    public static void Parse(string s)
    {
        throw new ArgumentException("Seconds from midnight cannot be more than 86400 in 010100712386401000000012");
    }
}

What happens if you do this?

[TestFixture]
public class CustomReferenceTests
{
    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void TestParsingCustomReferenceWithInValidSecondsFromMidnight()
    {
        // I am expecting this method to throw an ArgumentException:
        CustomReference.Parse("010100712386401000000012");
    }

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

public class CustomReference
{
    public static void Parse(string s)
    {
        throw new ArgumentException("Seconds from midnight cannot be more than 86400 in 010100712386401000000012");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文