Make Contract.Assert throw an exception rather than displaying a dialog box

If I use the new Code Contracts Contract.Assert method, can I make it throw an exception rather than displaying a dialog box? I want to do this when running unit tests on an assembly machine.

+7
source share
1 answer

Thanks to this post on the MSDN forums, I found a possible solution.

 namespace QuickGraph.Tests { [TestClass] public class AssemblyContextTest { [AssemblyInitialize] public static void Initialize(TestContext ctx) { // avoid contract violation kill the process Contract.ContractFailed += new EventHandler<ContractFailedEventArgs>(Contract_ContractFailed); } static void Contract_ContractFailed(object sender, System.Diagnostics.Contracts.ContractFailedEventArgs e) { e.SetHandled(); Assert.Fail("{0}: {1} {2}", e.FailureKind, e.Message, e.Condition); } } } 

It works.

+5
source

All Articles