locked
How to test private static method that has optional parameter RRS feed

  • Question

  • Hi,

    I have to write a unit test to test a private static method that has optional parameters.

    Below is the code snippet of method and Test Method -

    public class Class1
        {
            private static bool IsTrue(string input1, bool input2 = false)
            {
                return true;
            }
        }

    [TestMethod]
            public void TestMethod1()
            {
                try
                {
                    string input1 = "something";
                    bool expected = true;
    
                    PrivateType privateType = new PrivateType(typeof(Class1));
                    object[] parameters = new object[1];
                    parameters[0] = input1;
    
                    bool actual = (bool)privateType.InvokeStatic("IsTrue", new Type[] { typeof(string), typeof(bool) }, parameters);
    
                    Assert.AreEqual(expected, actual);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }


    This is working fine. But, my query is that should I provide input for optional parameter in my unit test?

    Regards,

    kvk1985

    Friday, July 29, 2016 9:57 AM

All replies

  • Hi kvk1985

    >>This is working fine. But, my query is that should I provide input for optional parameter in my unit test?

    Since you use "try...catch" in your test project, if I run your code without try-catch statement, it will report error,  so you should add input parameter to your test method.

    I have rewritten this code

    bool actual = (bool)privateType.InvokeStatic("IsTrue", new Type[] { typeof(string), typeof(bool) }, parameters);

    to

    bool actual = (bool)privateType.InvokeStatic("IsTrue", input1, false);

    and run it successfully.

    Here is the document about privateType.InvokeStatic():

    https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.privatetype.invokestatic.aspx       

    Best Regards,

    Jack 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 1, 2016 3:06 AM
  • Hi Jack,

    Thanks for the reply. I placed the try...catch block to understand why it was failing.

    So, ignore the try...catch block.

    But, my question is why should I give inputs for optional parameters in the unit test as I need not give the inputs when i am using that method in the code?

    Regards,

    kvk1985

    Monday, August 1, 2016 3:26 AM
  • Hi kvk1985

    >>My question is why should I give inputs for optional parameters in the unit test as I need not give the inputs when i am using that method in the code?

    You could define that method without parameters like

    private static bool IsTrue()

    Then add test code like this:
    bool actual = (bool)privateType.InvokeStatic("IsTrue");

    If you call a method that with input parameters, we should assign values to these parameters even if these parameters are not used inside the method body.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 1, 2016 6:43 AM
  • Hi Jack,

    I think my question is yet to be replied. So, I don't think your answer can be marked as answer.

    Regards,

    kvk1985

    Tuesday, August 9, 2016 12:40 AM
  • Hi kvk1985,

    Sorry for no help to you.

    Could you get helpful information from this case which also shared us how to test private static method that has optional parameter?

    http://stackoverflow.com/questions/29829859/mstest-privatetype-invokestatic-throws-missingmethodexception.

    I will also discuss this issue with language developing members, maybe the real issue would be related to the optional parameter usage or others except the unit test.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, August 9, 2016 12:23 PM
  • Hi kvk1985

    If you need not give the inputs, why in your method constains input parameters? 

    It is well known that if we call a method which contains input parameters. We should specify these parameters. In Unit test, when we test a method, we still need to call the method. So input parameters are needed.

    Tuesday, August 9, 2016 3:12 PM
  • Hi Jack,

    I am talking here about Optional parameters.  Optional parameters are 'optional'. I hope you understand that. So, if i provided input for the optional parameter, the method takes it and if I do not, then it goes ahead with the default value.

    So, i was expecting the same behavior when using PrivateType.

    Regards,

    kvk1985

    Thursday, August 11, 2016 3:24 AM
  • Hi kvk1985,

    Sorry for my misunderstanding to this issue.

    It would be a real unit test issue, not the Optional parameters issue.

    >>If i provided input for the optional parameter, the method takes it and if I do not, then it goes ahead with the default value.

    You are correct that it uses the default value if you don't provide the input, but it means that out test method would have the limitations if we don't provide the input.

    I mean that to really test a method, we need to think about all possible parameters, then we could make sure that our class method is really correct.

    So we need to provide one input, at the same time, we also need to provide the default value "false". Just those two parameters could really prove that the class method works normally in our unit test project. we need to think about the data integrity.

    A simple sample would be better for us to discuss this issue:

    For example, we test a login method, we need to use the correct username and password, but we also need to provide the wrong username and password, the login method was correct even if the test failed during use the wrong data.

    It would be similar to your test method optional parameters.

    If I misunderstood this issue, please feel free to let me know.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, August 12, 2016 5:59 AM