lundi 20 janvier 2020

Unit test of the class has many private methods and extensive logic

I have one public method that calls many private methods and these private methods set some values to request object. one of the private methods do API call using this object as a parameter and returns a response. Using this response value I call another private method do something.

On the below, you can see a sample structure of my class. Private methods are more than 4 methods and each one has extremely long logic and mainly related to this class. I don't use them in any other class. They only set lots of object properties. Some of them required fields for the API.

In the unit test, I mock the helper class and call the public method. In this way, I debug private methods but have to assign around 40 default values to call API and continue to test. Also, if I change some variables value, the expected results will be different.

As a result, how can I do effectively a unit test for this class? It feels something wrong but I don't know how can I change the structure of my class and test it? Also, testing like this way is an integration test or unit test? I am so confused. Any help would be appreciated.

public class SampleClassBase {
  have around 40 variables
}
 public class SampleClass : SampleClassBase
    {
        private IHelper _helper;
        private string someText;

        public SampleClass(IHelper helper)
        {
            _helper = helper;
        }
        public void DoSomething()
        {
            RequestObject obj = new RequestObject();
            SetAddress(ref obj);
            SetBillingInfo(ref obj);
            DoApiCall(ref obj);
        }

        private void SetAddress(ref RequestObject obj)
        {
            //set some values from SampleClass variables to obj fields. There are around 10-15 fields and have some null controls for them.  
            // There can be also another method call from helper class or base class
        }

        private void SetBillingInfo(RequestObject obj)
        {
              //set some values from SampleClass variables to obj fields.  There are around 10-15 fields and have some null controls for them.  
            // There can be also another method call from helper class or base class
        }

        private void DoApiCall(RequestObject obj)
        {
            // I mock this api call using moq framework. 
            var response = _helper.DoCall(obj);

            //Here I have lots of controls and finally it assign value to someText
            someText = response.value;
            SaveFile();
        }

        private void SaveFile()
        {
            //do things using someText property. Mainly saving a pdf file
        }

    }

Aucun commentaire:

Enregistrer un commentaire