I have the following method:
public static DateTime SubQtrs( this DateTime dt, int qtrs )
{
Contract.Requires( qtrs > -1 );
Contract.Requires( ( qtrs * 3 ) >= -120000 && ( qtrs * 3 ) <= 120000 );
// do something
}
I created a paramertized unit test and used Pex to come up with some unit test. As expected, Pex tested the contract constraints and passed in a qtrs value that violated the contract. I told the unit test to expect the exception with: [ExpectedException(typeof(TraceAssertionException))] and everything passed.
Now when I just run the unit test with the MS Test Harness (without Pex), I get an error: Descrtipion: Precondition failed: (qtrs * 3 >= -120000 && ......
It gives me the stack trace basically telling me the contracthelper failed. Then I'm given the choice to Abort, Retry, or Ignore.
Either way, the test fails and I get: Failed SubQtrsThrowsTraceAssertionException818 DGALib.Extensions.Tests Test method System.ExtensionMethodsTest.SubQtrsThrowsTraceAssertionException818 threw exception System.ArgumentOutOfRangeException, but exception Microsoft.Pex.Framework.Exceptions.TraceAssertionException was expected. Exception message: System.ArgumentOutOfRangeException: Months value must be between +/-120000. ...
Why is the unit test not recognizing the code contract error anymore?