gpt4 book ai didi

nunit - 如何使用 nunit 和 moq 进行异常处理?

转载 作者:行者123 更新时间:2023-12-04 16:37:37 26 4
gpt4 key购买 nike

我正在尝试使用 nunits 新的异常处理方式,但我发现很难找到有关它的信息以及如何将它与 moq 一起使用。

我现在有 moq 会在模拟方法上引发异常,但我不知道如何使用 nunit 来捕获它并查看它。

最佳答案

有几种不同的方法可以做到这一点;我使用 Assert.Throws。

var exception = Assert.Throws<YourTypeOfException>(()=> Action goes here);

例如
var exception = Assert
.Throws<ArgumentNullException>(()=> new ChimpPuncher(null));

然后,您可以根据需要进一步查询异常对象,例如
Assert.That(exception.Message, Text.Contains("paramname");

关于nunit - 如何使用 nunit 和 moq 进行异常处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1386421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com