为什么Console.WriteLine无法在MSpec断言中工作?

时间:2013-02-20 13:14:35

标签: console mspec

我在从MSpec断言写入控制台时遇到问题。我正在使用Nuget的v0.5.11(unsigned)。这里没有激进的东西,但它不起作用。谁知道我错过了什么?

public class When_doing_stuff
{
    It should_out_stuff_to_console = () =>
    {
        var val1 = 1;
        var val2 = 2;
        (val1 + val2).ShouldEqual(3);
        Console.WriteLine(val2); 
    };
}

2 个答案:

答案 0 :(得分:2)

我怀疑你的真实代码实际上有一个失败的断言。你发布了一个传递断言,应该打印得很好(因为亚历山大和我都在本地进行了验证)。

然而,失败的断言会抛出SpecificationException并且永远不会执行Console行。你应该在断言之前打印。例如

It should_out_stuff_to_console = () => 
{
    Console.WriteLine(val2);
    (val1 + val2).ShouldEqual(3);
}

mspec命令行运行器的输出

cmd> mspec-clr4.exe test.dll

Specs in test:

When doing stuff
Blah
» should out stuff to console

但是,我不建议从断言或测试运行中将调试语句打印到控制台。它会破坏测试报告(如上所示)。

答案 1 :(得分:0)

事实上,这里的断言并没有错,安东尼: - )

我将发布的代码user287079粘贴到新的类库中,查看我的控制台中正在打印的内容:

>mspec-clr4 <somewhere>\bin\Debug\ClassLibrary1.dll

Specs in ClassLibrary1:

When doing stuff
2
» should out stuff to console


Contexts: 1, Specifications: 1, Time: 0.11 seconds