コード例 #1
0
        public void ExportToFile_ReturnsCorrectOutputinStream()
        {
            var grader = new Grader();

            grader.ScoreList.Add(new PersonAndScore("BUNDY", "TERESA", 89));
            grader.ScoreList.Add(new PersonAndScore("BUNDY", "LUCY", 88));

            int expectedNumOfLines = grader.ScoreList.Count;

            using (var stream = new MemoryStream())
            {
                using (var streamWriter = new StreamWriter(stream))
                {
                    grader.ExportToFile(streamWriter);

                    //let's read what has been written

                    streamWriter.Flush();
                    stream.Position = 0;
                    using (var sr = new StreamReader(stream))
                    {
                        string output = sr.ReadToEnd();

                        //let's check output is as expected
                        string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
                        Assert.IsTrue(lines.Length == expectedNumOfLines + 1); //one more because of end of line at the end of the file
                        Assert.AreEqual("BUNDY" + SEPARATOR + "TERESA" + SEPARATOR + 89, lines[0]);
                        Assert.AreEqual("BUNDY" + SEPARATOR + "LUCY" + SEPARATOR + 88, lines[1]);
                    }
                }
            }
        }