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

How to mock private variable that in a static class

2 Answers 586 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Dino
Top achievements
Rank 1
Dino asked on 09 Feb 2017, 03:01 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Svetlozar
Telerik team
answered on 13 Feb 2017, 02:50 PM
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
0
Dino
Top achievements
Rank 1
answered on 16 Feb 2017, 01:09 AM
Thank you very much.
Tags
General Discussions
Asked by
Dino
Top achievements
Rank 1
Answers by
Svetlozar
Telerik team
Dino
Top achievements
Rank 1
Share this question
or