Unit Test Help. How do I test for a message output to console?

前端 未结 4 359
暖寄归人
暖寄归人 2021-01-17 21:55

I am a newbie to unit testing. How do I check the for console output? I have

namespace XXShapes
{
    public abstract class XXShape
    {
        public virt         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-01-17 22:12

    You don't need to test 'Console.WriteLine' routine because you have to assume it works - it is not your code, so why do you want to test it. You need to test whether you produce correct string that is passed to 'Console.WriteLine'

    In other words, instead of:

    public override void DrawXXShape()
    {
        Console.WriteLine("The XXCircle was drawn.");
    }
    

    you could do:

    public override void DrawXXShape()
    {
        Console.WriteLine(produceXxCircle());
    }
    
    public string produceXxCircle()
    {
        return "The XXCircle was drawn.";
    }
    

    and then in the test case:

    Assert.AreEqual(produceXxCircle(), "The XXCircle was drawn.");
    

    Hope it helps. Regads Simon

提交回复
热议问题