Mock Base Class Constructor

7 posts, 1 answers
  1. Kerri
    Kerri avatar
    3 posts
    Member since:
    May 2015

    Posted 15 Jun 2015 Link to this post

    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";
            }
        }

  2. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 16 Jun 2015 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Kerri
    Kerri avatar
    3 posts
    Member since:
    May 2015

    Posted 16 Jun 2015 in reply to Stefan Link to this post

    Thanks Stefan. That works perfectly!
  5. Kerri
    Kerri avatar
    3 posts
    Member since:
    May 2015

    Posted 17 Jun 2015 Link to this post

    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?

  6. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 17 Jun 2015 Link to this post

    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
  7. Nathan
    Nathan avatar
    1 posts
    Member since:
    Feb 2014

    Posted 16 Jul 2015 Link to this post

    Any idea when the relaxed API will be available for mocking internal/protected constructors?
  8. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 21 Jul 2015 Link to this post

    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
Back to Top
DevCraft R3 2016 release webinar banner