vendredi 3 mai 2019

Unit test v/s Integration test

I have a function which just reads a file line by line and does some logic for the output.

internal List<string> GetDataToTranslate(string filePath)
{
        List<string> dataToTranslate = new List<string>();

        using (StreamReader fileReader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), Encoding.GetEncoding("ISO-8859-1")))
        {
            string line, finalLine;
            StringBuilder sb = new StringBuilder();
            bool multiLineComment = false;

            while ((line = fileReader.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.StartsWith("'"))
                {
                    if (!multiLineComment)
                    {
                        multiLineComment = true;
                    }
                    sb.Append(line.Substring(1) + DELIMITER);
                }
                else
                {
                    if (multiLineComment)
                    {
                        finalLine = sb.ToString();
                        dataToTranslate.Add(finalLine.Substring(0, finalLine.Length - DELIMITER.Length));
                        sb = sb.Clear();
                    }
                    multiLineComment = false;
                }
            }
        }

        return dataToTranslate;
    }

The whole crux of the function is to do reading line by line from a stream, so does it even make sense to write a unit test for it? Should I just write an Integration test with a test file? Personally, I feel that I should not be writing a unit test because the method is totally dependant on an external file. Please suggest a best practice for this scenario.

Aucun commentaire:

Enregistrer un commentaire