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
Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Constructor - Override implementation, verify arguments

0 votes
Hello,

I have been looking all over but am unable to come up with a solution that overrides the constructor.

When creating an instance of a specified class I would like to be able to provide a different implementation of the constructor. Much the same as we can override the methods of a class, define a default fake instance, etc.

I have provided pseudo-typemock-code below which should make clear what I am trying to accomplish.

I would also like to verify the arguments that were provided when calling the constructor of an object. Similar to Isolate.Verify.WasCalledWithArguments for a constructor. I do not know the proper lambda expression to target constructors.

There are several old posts on the topic but no conclusive solution. Please help me out.
Cheers,
David

       
 [TestMethod]
        public void fakePublic()
        {
            var fakeDependency = Isolate.Fake.Instance<Dependency>(Members.CallOriginal, ConstructorWillBe.Called).doInstead(
(param1, param2) => {
 //IMPLEMENT A TEST CONSTRUCTOR HERE
}
);

            //Public Method
            Isolate.WhenCalled(() => new Dependency(5,”hi”)).DoInstead(
            (param1,param2) =>
            {
                //IMPLEMENT A TEST CONSTRUCTOR HERE
            }
            );

        }


public class Dependency
    {
        public int Age;
        public string Name;
        public Dependency(int age, string name)
        {
            Age = age;
            Name = name;
        }
        public int testMethod(int someint)
        {
            return 6;
        }
    }

asked Jan 15, 2013 by daskinne (1,680 points)

1 Answer

0 votes
Hi David,

Great questions!
Let's take them one by one:

1. Replacing a constructor: I'm assuming what you're trying to do is replace the future object (the dependency created in the code under test) with an object that's initialized with other parameters. At least that's what I get from your pseudo code.

To do this, you should either "new" your replacement dependency, or use Fake.Instance with the other parameters (just like a regular factory method). Then use Swap.NextInstance to replace the replacement dependency with the future one. Of course, you can replace any behavior on that replacement dependency with Isolate.WhenCalled.

2. Verifying parameters to a constructor: I'd try to stay away from this (partly because there's no direct API, but more on this later), by trying to assert on the dependency state instead. So, if for instance the parameters are exposed as properties, assert on them, or if your testMethod does something with them and returns them to the code under test, assert the impact.

It is usually a better idea to test the impact of parameters, rather than if they got passed. Constructors are even more susceptible because you may choose to change the code to use another constructor, and still get the same result. There's no need for the test to change for that. This is the thinking behind no API for constructors.

I'll be happy to answer more questions.

Gil
answered Jan 17, 2013 by gilz (14,460 points)
...