Search code examples
code-contractspexpex-and-moles

Pex not satisfying code contract


I'm trying to wrote a pex test, and I noticed that it always was feeding a false value as one of the params that I wanted. My test looked like this (simplified: there are/were more params, but otherwise no different):

[PexMethod]
public void TestCtor(bool value)
{
    ArbitraryType myType = new ArbitraryType(value);
}

I wanted to test a scenario where I would have pex do the exploration, ensuring that value would be true. I made another test that looked like this:

[PexMethod]
public void TestCtor(bool value)
{
    Contract.Requires(value == true);

    ArbitraryType myType = new ArbitraryType(value);
}

But when I have Pex explore that, it still spits in false to value and the test it generates "passes". If I put a line after the requirement saying Contract.Assert(!value); It'll create another test and pass true for value to fail the assertion.

The question is, why isn't Pex satisfying the code contract?


Solution

  • I'm not sure what Pex is going to do with Contracts in the test methods, but I can't see it being a Good Thing :)

    If you want Pex to do this, the correct thing to do is use PexAssume.IsTrue(value).