Skip to content

Instantly share code, notes, and snippets.

@FilipDeVos
Last active December 21, 2015 04:28
Show Gist options
  • Save FilipDeVos/6249219 to your computer and use it in GitHub Desktop.
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.)
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;
}
}
}
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