How to mole other methods in the class under testing RRS feed

  • Question

  • Hi,

    Just wondering if there is any way to mole other methods being used by the method under testing. For instance, in the following example, I am unit testing method1, which uses method2 in the same class. Can I mole method2?

    Thanks a lot.



    Class A


        private void Method1 ()



             val = Method2();




       private int Method2()








    Saturday, December 18, 2010 3:35 PM

All replies

  • Yes, but only if they are not private. No redirection framework that I'm aware of will redirect private methods (for a good reason too). If they were public or protected (or internal?) you'd be able to redirect them like any other method.
    Sunday, December 19, 2010 10:51 PM
  • Yes you can mole any method you want including private methods**.


    ** private methods are supported as long as all the types on the signature are visible.

    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Monday, January 10, 2011 10:39 PM
  • I have been trying test this but i am unable to get it working.

    Do you have a working example that demostrates this feature?



    Tuesday, January 18, 2011 3:46 PM
  • Try setting the InstanceBehavior of the moled instance to MoleBehaviors.FallThrough.
    The FallThrough behavior is documented: Get a behavior that calls into the original implementation when no delegate is provided.



    classAMole = new MClassA();
    classAMole.InstanceBehavior =

    classAMole.Method2 = () => 2;

    Tuesday, April 12, 2011 7:32 AM