This is a migrated thread and some comments may be shown as answers.

Mock Base Class Constructor

6 Answers 357 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kerri
Top achievements
Rank 1
Kerri asked on 15 Jun 2015, 08:55 AM

If given the following code, is it possible to mock the Base class constructor without mocking the derived class constructor so that MyProp gets set?:

    public class Base
    {
        public Base()
        {
            throw new NotImplementedException();
        }

        public string MyProp { get; set; }
    }

    public class Derived : Base
    {
        public Derived()
        {
            MyProp = "hello";
        }
    }

6 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 16 Jun 2015, 08:13 AM
Hi Kerri,

You can arrange the base constructor independently:

Mock.Arrange(() => new Base()).DoNothing();
var x = new Derived();

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Kerri
Top achievements
Rank 1
answered on 16 Jun 2015, 08:18 AM
Thanks Stefan. That works perfectly!
0
Kerri
Top achievements
Rank 1
answered on 17 Jun 2015, 10:18 AM

I have a further question on this topic. If the base class has an internal constructor, the above doesn't work unless the InternalsVisibleTo attribute is added to the assembly. If the assembly is signed with a strong key, then the test assembly will need to be too. This could quickly become quite an overhead when setting up tests.

Is there an easy alternative to this available?

0
Stefan
Telerik team
answered on 17 Jun 2015, 01:08 PM
Hi Kerry,

Due to a short-coming of the non-public mocking API, it is impossible to mock a non-public constructor right now. One of our upcoming releases will feature a relaxed API that allows constructor mocking. In the mean time your only choice is to use InternalsVisibleTo.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Nathan
Top achievements
Rank 1
answered on 16 Jul 2015, 10:55 PM
Any idea when the relaxed API will be available for mocking internal/protected constructors?
0
Stefan
Telerik team
answered on 21 Jul 2015, 07:36 AM
Hello Nathan,

The API will be made available in the upcoming service pack.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Kerri
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Kerri
Top achievements
Rank 1
Nathan
Top achievements
Rank 1
Share this question
or