Search code examples
mockingrhino-mockstypemock

Getting arguments of a called Typemock mocked method


I am a keen user of RhinoMocks developing from a TDD and AAA perspective with NUnit and ReSharper. I am changing jobs and the team I am moving to uses TypeMock so I want to hit the ground running... and I have run into a problem. How can I get the arguments for a called method on a mock object. When using RhinoMocks I use:

mockObject.GetArgumentsForCallsMadeOn(x => x.MethodIWantToGetParametersFrom(null))

Which returns a IList of type object array. Great! I go and get what I want and process it how I wish. Now using the AAA syntax of TypeMock I cannot seem to work out a way to do this... Could anyone shed some light on this please? Should I be doing it differently?

Thanks for reading and I look forward to your responses!

Adam


Solution

  • you can use DoInstead():

    Isolate.WhenCalled(()=>x.MethodIWantToGetParametersFrom).DoInstead(context => Console.WriteLine(context.Parameters[0].ToString())
    

    You get a Context object that contains the param values.

    you can also implement a method with the same name on your own class, and swap calls from the faked object to that method:

     class MyOwnClass
        {
        void MethodIWantTOGetParametersFrom(string s){
    Console.WriteLine(s);
    } //this is NOT the real method
        }
    
        //in test:
        MyOwnClass own = new MyOwnClass();
        Isolate.Swap.CallsOn(realClassInstance).WithCallsTo(own); //only methods that are implemented in the OwnCalss will be redirected. others will be called on the original instance.