You can hook into the Contract.ContractFailed
event.
This will be raised before the exception is thrown. You can combine this with catching Exception
to be pretty certain that it represented a contract failure
public void VerifyContract(Action action) {
bool failed = false;
bool thrown = false;
EventHandler e = (sender, e) => { failed = true; }
Contract.ContractFailed += e;
try {
action();
} catch (Execption) {
Assert.True(failed);
thrown = true;
} finally {
Contract.ContractFailed -= e;
}
Assert.True(thrown);
}