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!
0 votes
public class BaseClass
{
      protected BaseClass(ClassX objX, string text, long? id, string hash, int? id2)
        {
            ...Code...
        }
}

public class DerivedClass: BaseClass
{
        protected DerivedClass(ClassX objX, long derivedNumber1, string derivedText, bool derivedBool1, bool derivedBool2, long? derivedId1, string derivedHash1, int? derivedId2)
            : base(objX, derivedText, derivedId1, derivedHash1, derivedId2)
        {
            ...Code...
        }

       public static DerivedClass CreateDerivedObject(ClassX objX, long derivedNumber1, string derivedText, bool derivedBool1, bool derivedBool2, long? derivedId1, string derivedHash1, int? derivedId2)
       {
           return new  DerivedClass(objX, derivedNumber1, derivedText, derivedBool1, derivedBool2, derivedId1, derivedHash1, derivedId2);
       }
}


As its apparent, I have long constructors and performs some logic during initialization. I am testing 'DerivedClass' constructor and all I want is to mock out the BaseClass constructor.

Here's my test:
        public void DerivedClassConstructorTest()
        {
            ClassX objX = new ClassX();

            MockManager.Mock<BaseClass>().ExpectConstructor().Args(objX, "Derived Text 1", (long)10, "Hash string", 102);
            DerivedClass target = DerivedClass.CreateDerivedObject(objX , 8, "Derived Text 1", true, true, 10, "Hash string", 102);
            [some asserts]
         }


Even after removing a check for arguments against 'BaseClass' constrcutor, constructor is not mocked out. That is,
            MockManager.Mock<BaseClass>().ExpectConstructor();


This test does not mock 'BaseClass' constructor and fails as I am not mocking internals of BaseClass constructor.

Here I specifically want to target on logic that is in the 'DerivedClass' constructor and I have separate test for 'BaseClass' that tests its logic.

I also tried doing this in different way,

        public void DerivedClassConstructorTest()
        {
            ClassX objX = new ClassX();

            MockObject mockObj = MockManager.MockObject<DerivedClass>(Constructor.NotMocked);
            mockObj.CallBase.ExpectConstructorAlways();

            DerivedClass target = DerivedClass.CreateDerivedObject(objX , 8, "Derived Text 1", true, true, 10, "Hash string", 102);
            [some asserts]
         }


This too fails to mock 'BaseClass' constructor.

Actual code has even more layers, I mean what is a BaseClass here, is derived from some other class and so on, If there's a way I can mock all the constructors other than DerivedClass constructor, that would be even better.

Thanks,
Vikas
asked by (2.8k points)

1 Answer

0 votes
You can use CallBase to solve this problem.

If the calss under test looks like:
public class BaseClass
{
    public BaseClass()
    {
        throw new Exception("don't call me");
    }
}

public class DerivedClass : BaseClass
{
    public DerivedClass()
        : base()
    {

    }
}


Then using the following test code should work:
[TestMethod]
public void TestMethod()
{
    Mock mock = MockManager.Mock(typeof(DerivedClass));
    mock.CallBase.ExpectConstructorAlways();

    var dummy = new DerivedClass();
}
answered by (11.9k points)
...