Assume I have the following code:
public abstract class TemplateBase {
public void TemplateMethod() {
Operation(); }
protected abstract void Operation(); }
public sealed class Implementation : TemplateBase {
bool _alwaysTrue;
public Implementation(bool alwaysTrue) {
_alwaysTrue = alwaysTrue; }
[ContractInvariantMethod] void ObjectInvariant() {
Contract.Invariant(_alwaysTrue == true); }
protected override void Operation() {
_alwaysTrue = false; } }
[TestClass] public sealed class InvariantTest {
[TestMethod] public void Constructor() {
new Implementation(false); }
[TestMethod] public void Method() {
new Implementation(true).TemplateMethod(); } }
InvariantTest.Constructor always fails with an 'Invariant failed' exception.
How can I get InvariantTest.Method to fail based on the invariant?
I've set the runtime checking to full and even enabled 'Call-site Requires Checking', but even that does not help.