通过xUnit测试时,将StoryQ修复到输出控制台

时间:2019-08-09 14:52:47

标签: xunit storyq

将StoryQ(BDD框架)与xUnit一起使用时,控制台输出不起作用。这是解决方法

public class UnitTest
{
    private readonly ITestOutputHelper _output;

    public UnitTest(ITestOutputHelper output)
    {
        _output = output;
    }

    [Fact]
    public void Fact()
    {
        var consoleOut = new StringWriter();
        Console.SetOut(consoleOut);

        try
        {
            new Story("Some story")
                .InOrderTo("gain a shared understanding")
                .AsA("agile team")
                .IWant("document the behaviors for discussion")
                .WithScenario("some scenario")
                .Given(DoSomething)
                .When(DoSomething)
                .Then(DoSomething)
                .Execute();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
        finally
        {
            consoleOut.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList()
                .ForEach(l => _output.WriteLine(l));
        }
    }

    private void DoSomething()
    {
        //whatever
    }
}

0 个答案:

没有答案