locked
Using PEX with class method instead of a static method RRS feed

  • Question

  • I love the potential of PEX. However all the examples of PEX are for static methods that specify all their inputs via parameters. Can Pex be used to test/explore a Class method where the class has a number of different fields, some of which may be used in the method undertest? If So How? The only way I have succeeded in getting pex to generate any tests for class methods is to write a test method that creates the class and executes the method. At which point I am not sure if PEX is exploring only the code in the wrapper method or is it trying to explore the code being referenced by the call to the method of the class I want to test.
    Thursday, February 19, 2009 4:16 AM

Answers

  • When you apply Pex to instance methods or methods that take objects as parameters, Pex will have to find a way to create the object, i.e. a sequence of public API calls that assign the fields of the class.

    Pex comes with a number of built-in heuristic (i.e. mini static analysis engines) to figure out a way to build most types but sometimes we need help from the user. In that case, you should see a 'Object Creation' bold text in the issue bar showing up. It will give you message such as 'guessed how to build type yyy'. Click on this message and select the 'Save Factory' method.

    Pex will create a static method factory for the given type. Something like

    [PexFactoryClass] // this attribute will be deprecated
    public
     static FooFactory  
    {  
        [PexFactoryMethod]  
        public static Foo CreateFoo(Bar bar, int i) {  
            // do whatever it takes to create foo.  
            var foo = new Foo(bar);  
            foo.Add(i);  
            return foo;  
        }  

    Pex will automatically use this factory on the next run. Notice that in this example, you would need a Bar type to create the Foo type. Pex will detect that and start building Bar instance to feed it into the Foo factory method, etc...

    Does this answer your question?
    Jonathan de Halleux
    Thursday, February 19, 2009 8:38 AM