a. Make sure an exception was thrown
b. After it was thrown, make sure nothing else happened.
This is what happens when you throw exceptions from SQL Stored Procedures.
The Problem
The standard way of checking for exceptions in NUnit are thus:1. ExpectedException attribute on the test method
2. Have your own try/catch block in your test method.
The problem with option 1 is that no assertions can be checked after the exception. The code runs until the exception is thrown, then you are done. That isn't what I need.
The problem with option 2 is that it is ugly. Yes, I'm being a cry baby here. I think the try catch throws more code in the test than I think should be there.
My Solution
So I created a new method called ExpectException. Here is the code:1: public T ExpectException<T>(MyMethod method) where T : Exception
2: {
3: try
4: {
5: method.Invoke();
6: }
7: catch(T e)
8: {
9: return e;
10: }
11: return null;
12: }
So now in my case, I have a stored procedure that throws an SqlException. I want to call the method and return the exception so I can check the exception for specific values (make sure the message is correct). Also, if any other exception is thrown, say a Exception -- not a SqlException, the test will fail.
Here is my new code:
1: SqlException e = ExpectException<SqlException>(delegate { ReplaceUnitCall(oldUnit, newUnit); });
2: Assert.IsNotNull(e);
So as long as you are familiar with anonymous delegates, this is a pretty nice solution -- to me.
No comments:
Post a Comment