Mocking Static Private Constructor

2 posts, 0 answers
  1. Danny
    Danny avatar
    1 posts
    Member since:
    Sep 2011

    Posted 18 Oct 2011 Link to this post


    Is there a way to mock a private static constructor?

    I got an static class with a private static constructor and I don't want to execute it because it configures himself from a configuration within the app.config.

    I just want to mock one of is method but the constructor is always call causing the test to throw an exception.

    Here an example thats illustrate my problem:

    I got a static class like this

    public static class StaticClassWithPrivateStaticConstructor
        private static string _text = "Text";
        static StaticClassWithPrivateStaticConstructor()
            throw new Exception();
        public static string Text
            get { return _text; }

    I want to be able to test Text property without executing the static constructor.

    public void MockStaticConstructor()
        Assert.AreEqual("Text", StaticClassWithPrivateStaticConstructor.Text);

    Is there a way to do this?

  2. Ricky
    Ricky avatar
    467 posts

    Posted 20 Oct 2011 Link to this post

    Hi Danny,

    Thanks again for contacting us.

    I checked the issue and it is possible to fake out the static constructor while you do Mock.SetupStatic. In that case, i would ask you to create a ticket so that i can send you an internal build with the feature included (you need to create the ticket (addressed to me) since i can't send internal builds to public forum).


    Kind Regards,
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top