samedi 27 février 2016

How to break dependency on StreamReader's readLine() method in microsoft fakes unit testing?

Here is my code in one of the method which I want to test:

    using (var sr = new StreamReader(myFile))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Equals("completed"))
                {
                    continue; //here it is getting called infinite times
                }

                if(line.Equals("processed"))
                {
                    break;
                }
            }
        }

In my test method I have written below code with the help of shim:

            ShimStreamReader.ConstructorString = delegate (StreamReader @this, string @string)
            {
                var reader = new ShimStreamReader(@this);
                reader.ReadLine = () =>
                {
                    return "completed";
                };
            };

Now I dont want to pass the file. Instead I want to pass the stream of characters or string. Above test code is calling and breaking the dependency of new StreamReader(myFile) as expected and entering into while loop. As soon as it is entering it in while loop, sr.ReadLine() is returning "completed" all the time. So I'm stuck here. How would I stop here or how would I write the input string so that as soon as my first call return completed in second call of sr.ReadLine() it should return null, and then breaks the loop?

Aucun commentaire:

Enregistrer un commentaire