mercredi 20 avril 2016

NunitTest a ConsoleOutput

I'm trying to Nunit Test a method, where I'm asserting that I'm getting the correct output, but the problem is each time the code jumps into :

Console.SetCursorPosition(xOffset, yOffset++);

And appearently you can't redirect SetCursorPosition, So my question is:.." is there anyway I can unit test this method reasonably"

    public void Execute()
    {
        int xOffset = 84; 
        int yOffset = 3;
        Console.SetCursorPosition(xOffset, yOffset++);
        Console.WriteLine("Events");

        string header = "| LogType         | Message      |          tagCollection |          Time |";

        Console.SetCursorPosition(xOffset, yOffset++);
        Console.WriteLine(header);

        Console.SetCursorPosition(xOffset, yOffset++);
        Console.WriteLine(new string('-', header.Length));

        if (!string.IsNullOrWhiteSpace(Status))
        {
            Console.SetCursorPosition(xOffset, yOffset++);
            Console.WriteLine(Status);
        }

        foreach (string str in NotificationList)
        {
            Console.SetCursorPosition(xOffset, yOffset++);
            Console.WriteLine(str);
        }
    }

Here is my test:

    [Test]
    public void NotificationDisplayer_inputTestString_ExpectedResult()
    {

        using (StringWriter sw = new StringWriter())
        {
            Console.SetOut(sw);
            uutNotificationDisplayer.Execute();
            Assert.That(sw.ToString()), Is.EqualTo(expectedResult));
        }
    }

Aucun commentaire:

Enregistrer un commentaire