Pex - Automated Whitebox Testing for .NET : Creatables

Creatables


Pex can reliably create values of all primitive types, arrays, and types with a visible constructors if all fields are either visible themselves, or can be unconditionally set via some visible property setter or method. However, if a value must be created in a more complicated way, e.g. by calling factory methods and setting properties that perform argument validation, then Pex needs more information.

Types representing data types are called 'creables'. This means that there is a direct mapping from a constructor or a factory method to the field of that type. Creatable types can be specified through attributes inherited from PexCreatableAttributeBase.

Note: Use this attribute only if Pex may set all fields of a class in any (type-correct) way. Consider using an attribute deriving from PexExplorableAttributeBase if in doubt.

  • creatable through public fields
public class Point {
    public int X,Y;
}
  • creatable through constructor
public class Point {
    public readonly int X;
    public readonly int Y;
 
    public Point(int x, int y) {
       this.X = x;
       this.Y = y;
    }
}
// binding
[PexCreatableByConstructor(typeof(Point), typeof(int), typeof(int))]
  • creatable through static method factory
public class Point {
    ...
    public static Point Create(int x, int y) {
       Point point = new Point();
       point.X = x;
       point.Y = y;
       return point;
    }
}
// binding
[PexCreatableByMethod(typeof(Point), "Create", typeof(int), typeof(int))]
  • creatable as singleton
public class Point {
     private Point() {...}
     public static Point Instance { get {...} }
}
// binding
[PexCreatableAsSingleton(typeof(Point), "Instance")]

Attributes

Pex comes with the following set of derived attributes:

Definining multiple creatables at once

If you want to specify that many types, which follow a common pattern, are creatable, such as Web Service data objects, see PexCreatableFactoryAttributeBase (and more specifically PexCreatablesAsXmlFromAssemblyAttribute).

See Also
IPexCreatable, IPexCreatableFactory
(c) Microsoft Corporation. All rights reserved. pex Wiki Documentation 0.93.50813.0