In order to make a custom Assert method as operate exactly like the standard assert methods, you must throw a new AssertFailedException
. At first I really didn't like this because the debugger stops on the AssertFailedException
throw statement, and not on the actual assert statement. After a little more research I discovered the DebuggerHidden
method attribute and viola, my assert performs as desired.
[DebuggerHidden]
public static void Contains(File file, string text){
if(!ContainText(file, text)){
HandleFail("MyAssert.Contains", null, null);
}
}
[DebuggerHidden]
private static void HandleFail(string assertName, string message, params object[] parameters )
{
message = message ?? String.Empty;
if (parameters == null)
{
throw new AssertFailedException(String.Format("{0} failed. {1}", assertName, message));
}
else
{
throw new AssertFailedException(String.Format("{0} failed. {1}", assertName, String.Format(message, parameters)));
}
}