/// <summary> /// Checks that the inputted parameters does not cause the inputted function to throw an exception OR raise a debug assert. /// If either of these things happen, the test will fail. /// </summary> /// <param name="functionToTest"></param> /// <param name="arguments"></param> public static object DoesNotThrow(Delegate functionToTest, params object[] arguments) { CatchTraceListener assertListener = new CatchTraceListener(); // Set up the assertion framework to just use our listener Trace.Listeners.Clear(); Trace.Listeners.Add(assertListener); object result = null; try { result = functionToTest.DynamicInvoke(arguments); } catch (Exception e) { // If we have thrown an exception the test should fail Assert.Fail("Exception thrown with message: " + e.Message); } Assert.IsFalse(assertListener.AssertionCaught, "Assertion raised"); // Reset it so that asserts still happen for the remainder of our test run Trace.Refresh(); return(result); }
/// <summary> /// Ensure that the arguments passed to the method raise a Debug assertion. /// Will catch the dialog so that it does not block our tests from continuing. /// </summary> /// <param name="functionToTest">Will have to pass in the method group casted to the appropriate Func. /// e.g. public int MyFunction(string str, bool result) -> AssertExt.AssertDebugAssertionRaised((Func<str, bool, int>)MyFunction);</param> /// <param name="arguments"></param> public static void DebugAssertionRaised(Delegate functionToTest, params object[] arguments) { CatchTraceListener assertListener = new CatchTraceListener(); // Set up the assertion framework to just use our listener Trace.Listeners.Clear(); Trace.Listeners.Add(assertListener); try { functionToTest.DynamicInvoke(arguments); } catch { // If an assert fires, it is likely an exception was also caused. // Swallow the exception for now } Assert.IsTrue(assertListener.AssertionCaught); // Reset it so that asserts still happen for the remainder of our test run Trace.Refresh(); }