Last active
December 21, 2015 04:28
-
-
Save FilipDeVos/6249219 to your computer and use it in GitHub Desktop.
Wrapper class to disable debug assert ui from popping up in unit tests. (I know it should not be needed, but you know how it goes.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Linq; | |
public class DebugAssertDialogDisabler : IDisposable | |
{ | |
private readonly DefaultTraceListener _alteredListener; | |
public DebugAssertDialogDisabler() | |
{ | |
foreach (var tempListener in Debug.Listeners.OfType<DefaultTraceListener>()) | |
{ | |
if (tempListener.AssertUiEnabled) | |
{ | |
_alteredListener = tempListener; | |
tempListener.AssertUiEnabled = false; | |
} | |
return; | |
} | |
} | |
public void Dispose() | |
{ | |
if (null != _alteredListener) | |
{ | |
_alteredListener.AssertUiEnabled = true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using NUnit.Framework; | |
public class ExampleTests | |
{ | |
private void SirAssertsALot() | |
{ | |
Debug.Fail("kaboom"); | |
} | |
[Test] | |
public void DebugAssertDialogShouldNotPop() | |
{ | |
// arrange | |
using (new DebugAssertDialogDisabler()) | |
{ | |
// act | |
SirAssertsALot(); | |
// assert | |
Assert.That(true, Is.Not.False); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment