We have just released a new version of Pex, v0.14.040610.2. This version brings a range of Bug Fixes that were reported on the MSDN forums and a couple new features: Fix Suggestions for Invariant Methods and Natural properties for Stubs.
Fix Suggestions for Invariant Methods
When you use Code Contracts, then Pex can now help you to add missing invariants, just as Pex already inferred missing preconditions in the past. Take for example the ArrayList class that comes with the samples of our recently updated tutorial slide deck. This class has the following fields:

When you add an empty invariant method,

and enable Runtime Checking of contracts, then Pex will create instances of this class using reflection while making sure that the invariants holds (we announced this feature in the last release). However, with an empty invariant method, Pex will be able to create illegal states, e.g. where the _items array is null, which may cause a NullReferenceException, and other issues. To get the following test cases, we let Pex explore the Add method of our ArrayList class:

When you look at the details of the failing test cases, then you will see that Pex now offers the option to add invariants directly into your [ContractInvariantMethod]:
Natural Properties for Stubs
The logic to handle properties with setters and getters generated by Stubs has been slightly improved so that it is possible to force properties to have as if they had a backing field.
When a getter or a setter of a property is invoked, Stubs checks whether the getter delegate or the setter delegates have been set. If not, i.e. no custom user behavior, it queries the fallback behavior for an initial value of the property. If the fallback behavior successfully returns a value, Stubs generates a closure and binds the getter and setter.
For example, here’s a simple IFoo interface with a Bar property:
One could write the following parameterized unit test:
Since we expect the Bar property to behave as it was wrapping a field, we don’t expect the assertion to be raised. When we execute this test with Pex, and the Pex fallback behavior, Pex will choose a value for ‘Bar’, and this value will be ‘stored’ the property. Therefore, we get as expected 2 test cases:
Non default constructor support for Stubs
In previous versions, Stubs could not generate stubs for classes with no default constructor. This is no longer the case, Stubs will generate one public constructor for each public or protected base constructor.
Bug Fixes
- Various code generation tweaks to make StyleCop happier
- Visual Studio addin crashes when loading in VS2010
- Visual Studio addin does not find attributes when they are globally qualified
- Added missing substitutions for System.Threading.Interlocked members
- Stubs automatically imports missing references
- When an object has an invariant method (from Code Contracts), Pex would emit assertions using the private fields
- Editorial issues to patterns paper
- Contracts no longer kill the process when the runtime rewriter was not executed (.net 4.0)
- Fixed bug in generic type guesser
- Fixed samples for the latest version of Stubs
As always, don’t hesitate to send us your feedback, good or bad, through our MSDN forums.