How to mock private variable that in a static class

3 posts, 1 answers
  1. Dino
    Dino avatar
    2 posts
    Member since:
    May 2016

    Posted 08 Feb Link to this post

    There is a private variable that in a static class:

    public static class Common     {   

            private static EMailSetting _mailSetting;

    ......

            public void BeTest()

            {

                    if(_mailSetting=null){

                       //do something

                   }

                   ........

            }

    }

    How can I  mock "_mailSetting"?

    I try this code, It's not work:

    Mock.SetupStatic(typeof(Common), Behavior.CallOriginal, StaticConstructor.Mocked);             

    Mock.NonPublic.Arrange<EMailSetting>(typeof(Common), "_mailSetting")                 

        .Returns(() =>                 {                    

                var mockMailSetting = Mock.Create<EMailSetting>();                     

                mockMailSetting.mailFrom = "C";                     

                mockMailSetting.mailTo = "D";                

                return mockMailSetting;                 

    });

    Common.BeTest();

  2. Answer
    Svetlozar
    Admin
    Svetlozar avatar
    294 posts

    Posted 13 Feb Link to this post

    Hello,

    Thank you for the great question.

    You can not mock fields directly with JustMock. We intercept method implementations, but fields don't have such. On the other hand you can mock properties and events, because internally they have get/set and add/remove methods.  

    For the example you've provided I suggest you change the _mailSetting field to be a private property. Here is the complete sample in a console app.

    class Program
    {
        static void Main(string[] args)
        {
            Mock.NonPublic.Arrange<EMailSetting>(typeof(Common), "_mailSetting")
                .Returns(() => {
                    var mockMailSetting = Mock.Create<EMailSetting>();
                    mockMailSetting.mailFrom = "C";
                    mockMailSetting.mailTo = "D";
                    return mockMailSetting;
                });
            Common.BeTest();
        }
    }
     
    internal class EMailSetting
    {
        public string mailFrom { get; set; }
        public string mailTo { get; set; }
    }
     
    public static class Common
    {
        private static EMailSetting _mailSetting { get; set; }
     
        public static void BeTest()
        {
            if (_mailSetting != null)
            {
                Console.WriteLine(_mailSetting.mailFrom);
            }
        }
    }

    Let me know if you have any questions.

    Regards,
    Svetlozar
    Telerik by Progress
    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. Dino
    Dino avatar
    2 posts
    Member since:
    May 2016

    Posted 15 Feb in reply to Svetlozar Link to this post

    Thank you very much.
Back to Top