How to handle exception handling with nunit and moq?

I am trying to use nunits a new way to handle exceptions, but I find it difficult to find information about this and how to use it with moq.

I now have moq that throws an exception in the mocked method, but I don’t know how to use nunit to catch it and look at it.

+5
source share
3 answers

There are several different ways to do this; I am using Assert.Throws.

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

eg.

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

Then you can request an exception object if you like, for example

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

Best way to mention: [ExpectedException(typeof(ApplicationException))]over the testing method.

+2
source

try/catch ?

0

All Articles