我知道并已经了解XUnit的ITestOutputHelper
和Debug.Trace
方法,但是我想知道是否还有其他可能的方式允许我在下面的代码中使用静态记录器,也许是Xunit有一个我不知道的可扩展性点?
我有一个扩展方法,允许我编写BDD样式的单元测试,如果我可以将每个步骤都打印到输出窗口,那就太好了
internal static class Spec
{
internal static void x(this string step, Action x)
{
try
{
x();
//print step to the output window
}
catch (Exception ex)
{
throw new Exception($"\n\n{step} failed\n", ex);
}
}
internal static void x(this string step)
{
}
}
上面的代码允许我这样编写测试:
[Theory]
[AutoMoqData]
public void CorrelationId_WhenCorrelationIdIsPassedIn(
string result,
string correlationId,
[Frozen(Matching.DirectBaseType)] DefaultHttpContext httpContext,
[Frozen] Mock<IHttpContextAccessor> httpContextAccessorMock,
CorrelationIdProvider sut)
{
$"Given a correlation id header value of {correlationId} has been passed in"
.x(() =>
{
httpContext.Request.Headers["x-Correlation-ID"] = correlationId;
httpContextAccessorMock.Setup(x => x.HttpContext).Returns(httpContext);
});
"When the correlation id is retrieved"
.x(() => result = sut.CorrelationId);
"Then the correlation id is not empty"
.x(() => result.Should().NotBeEmpty());
$"And the correlation id matches {correlationId}"
.x(() => result.Should().Be(result));
}