locked
How do I delegate a mole to use a methods original behavior RRS feed

  • Question

  • I am in the process of comparing Moles to TypeMock.  I have reached a snag, when I have a class that has two methods in which method A calls Method B and Method B is mocked.  TypeMock had a feature called CallOrginal which would be applied to Method A.  I have tried to use ExecuteWithoutMoles however I get an error message that this method has not been moled.  Code snippet is attached.  Thanks in advance.

    // Arrange
                // 1. Create the Mole Object
                var targetmole = new MLabTest();

                // 2. Establish a delegate.  In this case the parameters must match.
                targetmole.GetValueInt32Int32 = (PatientId, LabTestId) => 50;
                //targetmole.GetTheHighestLab  ;
                MolesContext.ExecuteWithoutMoles(() => targetmole.Instance.GetTheHighestLab());
                // Act
                int expected = targetmole.Instance.GetTheHighestLab();
                // Assert
                Assert.AreEqual(expected, -50);

    Anthony
    Friday, January 7, 2011 7:45 PM

All replies

  • It does not make sense to call any method on the MLabTest instance: no constructor were executed on this instance. Leaving that detail aside, I would expect that this call works without issue:

     MolesContext.ExecuteWithoutMoles(() => targetmole.Instance.GetTheHighestLab());

    but this one does not and throws the 'MoleNotImplementException' since you are effectively trying to call a method on a uninitialized instance.

     int expected = targetmole.Instance.GetTheHighestLab();

    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Monday, January 10, 2011 11:07 PM
  • Here's an example that would make more sense:

                     var lab  = new LabTest(); // some instance created somewhere

          // 1. Create the Mole Object
                var targetmole = new MLabTest(lab);

                // 2. Establish a delegate.  In this case the parameters must match.
                targetmole.GetValueInt32Int32 = (PatientId, LabTestId) => 50;


    Jonathan "Peli" de Halleux - Try Pex online at www.pexforfun.com!
    Monday, January 10, 2011 11:09 PM
  • Thanks for the reply.  I appreciate the help, however I changed the code (this makes sense) but I am still getting method has not been moled exception:

     

    // Arrange
                // 1. Create the Mole Object
                var target = new LabTest();
                var targetmole = new MLabTest(target);
                // 2. Establish a delegate.  In this case the parameters must match.
                targetmole.GetValueInt32Int32 = (PatientId, LabTestId) => 50;
                // 3. Trying to execute the original code
                MolesContext.ExecuteWithoutMoles(() => targetmole.Instance.GetTheHighestLab());
                // Act
                int expected = targetmole.Instance.GetTheHighestLab();
                // Assert
                Assert.AreEqual(expected, -50);

    Anthony
    Tuesday, January 11, 2011 1:24 PM
  • Hi adangelo, how is the comparision going?

    I am also looking for the right mock framework, the big inconvenient I am finding about Moles (and Pex) is a 'weak' documentation and a lack of more examples, let's wait for future releases and see if the community accepts and backs the project giving it more support..

    I had the same problem as you and I solved it using AllInstances property of the mock.

    (I haven't tested this code but should work, sorry..)

    // 1. Create the Mole Object      
    var target = new LabTest();      
         
    // 2. Establish a delegate. In this case the parameters must match.      
    MLabTest.AllInstances.GetValueInt32Int32 = 
    (PatientId, LabTestId) => 50;      
    // Act      
    int expected = target.GetTheHighestLab();      
    // Assert      
    Assert.AreEqual(expected, -50);
    

    The only problem I see of this approach, is that you will handle any calling you are doing to GetValue() with any instance of LabTest class.

    Regards

     

    Tuesday, January 25, 2011 5:15 PM
  • Thanks, I am going to give it a try.  I believe we are going to take the $$$ plunge and go with TypeMock.  Its a more mature product and a bit more intuitive.  If developers are going to have to spend creating unit tests they are not going to do it.  I will let you know if your code worked or not.


    Anthony
    Tuesday, January 25, 2011 5:55 PM
  • Thanks, I am going to give it a try.  I believe we are going to take the $$$ plunge and go with TypeMock.  Its a more mature product and a bit more intuitive.  If developers are going to have to spend creating unit tests they are not going to do it.  I will let you know if your code worked or not.


    Anthony


    That's right but take into account they don't support -yet- many mscorlib classes (such as MemoryStream, for instance), despite they say the do (http://forums.typemock.com/viewtopic.php?t=2076)

    I am using Moles and it is great (there is still a long way to be a mature product as TypeMock), but I might be missing something about Pex, because I don't find it very helpful.

    Tutorials only show easy examples, but I am still struggling with new features derived from parameterized test, such as Factory creations and the likes.. Everytime I run pex, it starts requesting for all of them and it takes me more time to find out how to create them than implementing my own test.. And Pex always ends up sending null parameters or reaching the maximum size of arrays.. 

    As saying, I am still learning!

    Wednesday, January 26, 2011 9:07 AM