单元测试帮助。如何测试一个消息输出到控制台? [英] Unit Test Help. How do I test for a message output to console?
问题描述
我是一个新手,单元测试。我如何检查控制台输出?
我有
I am a newbie to unit testing. How do I check the for console output? I have
namespace XXShapes
{
public abstract class XXShape
{
public virtual void DrawXXShape()
{
Console.WriteLine("The XXShape was drawn.");
}
}
public class XXCircle : XXShape
{
public override void DrawXXShape()
{
Console.WriteLine("The XXCircle was drawn.");
}
}
}
}
namespace XXShapes.Test
{
[TestFixture]
public class XXShapeTest
{
[Test]
public void MyFirstTest()
{
XXShape s = new XXCircle();
string expected = "The XXCircle was drawn.";
s.DrawXXShape();
string actual = Console.ReadLine();
Assert.AreEqual(expected, actual);
}
}
}
我应该如何正确地测试这个?
感谢您的任何指针。
干杯,
〜CK
How should I correctly be testing this? Thanks for any pointers. Cheers, ~ck
推荐答案
字面的答案是,你会使用 Console.SetOut
调用之前所测试的类直接标准输出
到MemoryStream或相似,其内容可以在以后检查。
The literal answer would be that you would use Console.SetOut
before calling the class under test to direct stdout
into a memoryStream or similar, whose contents you can later inspect.
更好的答案是使用一个模拟框架,像犀牛制品创建抽象类的具体实例,期望设置了 DrawXXShape
方法将被调用。
The better answer would be to use a mocking framework, like Rhino Mocks to create a concrete instance of your abstract class, with an expectation set that the DrawXXShape
method would be called.
这篇关于单元测试帮助。如何测试一个消息输出到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!