chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I was also wondering if I have missed something in the API for batch assignment of return values, method reassignment, etc.

While we can use ReturnRecursiveFakes to simplify a lot of the moles code; moles did allow some efficiency if we want to say override 6 methods in one context. From my understanding typemock requires us to repeat ‘Isolate.Action(target).Routing’ for each change within the class. While this is much easier to understand than moles I anticipate this leading to a lot of bloat for complex classes. Is there something in place to deal with larger objects?

An example of moles method reassignment is included below:

            
MolesPlugins.MPlugin.Constructor
                = (DataCubePlugin @this) =>
                {
                    var mole = new MolesPlugins.MPlugin(@this)
                    {
                        GetIdValue = () => expectedGridID,
                        IdGet = () => expectedGridID,
                        Activate = () => { },
                        GetObjectString = (string type) => { return new object[] { grid }; },
                        GetMax = () => { return grid.GetMax(); }
                    };
                };




With Typemock:

var mole = Isolate.Fake.Instance<MolesPLugins.MPlugin>(Members.CallOriginal, ConstructorWillBe.Ignored);
Isolate.WhenCalled(() => mole.GetIdValue).WillReturn(expectedGridID);
Isolate.WhenCalled(() => mole.Id).WillReturn(expectedGridID);
Isolate.WhenCalled(() => mole.Activate).WillReturn();
Isolate.WhenCalled(() => mole.GetObjectString).WillReturn(new object[] { grid };);
Isolate.WhenCalled(() => mole.GetMax).WillReturn(grid.GetMax());


Is it also possible to overwrite methods for all instances of a class at one time?
Or... must I first generate a redirected class and then call
Isolate.Swap.AllInstances<Class>().With(redirected_instance);
asked by daskinne (1.7k points)

1 Answer

0 votes
Hi David,

The way you described is the way to go. There is a bit of a change for read/write properties on fake objects though. You can use:

fake.Prop = "fakeReturnValue";


Which is equivalent to
Isolate.WhenCalled (()=> fake.Prop).WillReturn("fakeReturnValue");


You are also correct about applying the same behavior to multiple instances. The shortest way to do that (which means all instance get the exact same behavior) is with SwapAllInstances.

Thanks,
answered by gilz (14.5k points)
...