Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing for inner exceptions

Tags:

I am writing some unit tests using Visual Studio's integrated framework. I need to write some test cases which pass when a proper exception is thrown. The problem is that the exceptions i need to test for are inner exceptions nested in a more general one. Is there some easy solution or do I need to extend the whole functionality. I am currently using the [ExpectedException] attribute, but it wont do much good in such a situation.

I am also curious what happens when we use [ExpectedException] while we also have some Assert logic in the test itself. Are both the conditions evaluated(exception was thrown and the Assert statement turned out to be valid) or the test passes immediately after the correct exception is thrown?

like image 798
zhulien Avatar asked Jan 26 '15 14:01

zhulien


2 Answers

Not a total solution, but in NUnit, you can do this sort of thing:

 var ex = Assert.Throws<Exception>(() => thing.ThatThrows());  Assert.That(ex.InnerException, Is.TypeOf<BadException>() ); 

Maybe you can in your test framework?

like image 123
Loofer Avatar answered Oct 03 '22 23:10

Loofer


If your framework doesn't support custom throwing, you usually have two choices:

  1. Implement it yourself
  2. Change (or extend) framework

I'll start with second solution. Consider using FluentAssertions library. It allows you to do something like this:

Action deleteUser = () => usersRepository.Delete(new User { Id = null });  deleteUser     .ShouldThrow<UserNotFoundException>()     .WithInnerException<ArgumentNullException>()     .WithInnerMessage("User Id must have value"); 

You will still use Visual Studio testing framework, just that you'll have one extra library for, well - fluent assertions.

First choice on the other hand is a bit more work as it is usually the case with hand-rolled solutions:

try {     usersRepository.Delete(new User { Id = null });     Assert.Fail("Deleting user with null id should throw"); } catch (UserNotFoundException ue) {     Assert.AreEqual(ue.InnerException.Message, "User Id must have value"); } 

You replace ExpectedException attribute with custom code asserting actual exception instance. Like I said, it is more work but does the trick.

like image 35
k.m Avatar answered Oct 03 '22 23:10

k.m