本文介绍了如何使用nunit和moq进行异常处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use nunits new way of exception handling but I am finding it hard to find information on it and how to also use it with moq.

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

I have right now moq that throws a exception on a mocked method but I don't know how to use nunit to catch it and look at it.

推荐答案

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

There's a few different ways to do it; I use Assert.Throws.

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

例如

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

然后,您可以根据需要进一步查询异常对象,例如

You can then query the exception object further if you want, e.g.

Assert.That(exception.Message, Text.Contains("paramname");

这篇关于如何使用nunit和moq进行异常处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:33