Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do exception handling with nunit and moq?

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.

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.

like image 421
chobo2 Avatar asked Sep 06 '09 19:09

chobo2


2 Answers

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

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

e.g.

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");
like image 123
Mark Simpson Avatar answered Jan 03 '23 18:01

Mark Simpson


Best way to mention is: [ExpectedException(typeof(ApplicationException))] above the test method.

like image 30
giri77 Avatar answered Jan 03 '23 18:01

giri77