public static void Throws <T>(Action action, string expectedMessage) where T : Exception { AssertAnswer answer = AssertAction <T>(action, expectedMessage); Assert.IsTrue(answer.Success); Assert.AreEqual(expectedMessage, answer.Message); }
public static AssertAnswer AssertAction <T>(Action action, string expectedMessage) where T : Exception { AssertAnswer answer = new AssertAnswer(); try { action.Invoke(); answer.Success = false; answer.Message = string.Format("Exception of type {0} should be thrown.", typeof(T)); } catch (T exc) { answer.Success = true; answer.Message = expectedMessage; } catch (Exception e) { answer.Success = false; answer.Message = string.Format("A different Exception was thrown {0}.", e.GetType()); } return(answer); }