[英]How to unit test a TextOutputFormatter
我已經編寫了一個按預期工作的TextOutputFormatter
,但現在我想圍繞它進行一些單元測試。 WriteResponseBodyAsync
的第一個參數是一個OutputFromatterWriteContext
,我不確定如何實例化。
我嘗試使用以下內容,但context.HttpContext.Response.Body
最終成為空流。
[Fact]
public async Task ShouldPrefixAsync()
{
// Arrange
var output = Guid.NewGuid().ToString();
var context = new OutputFormatterWriteContext(
new DefaultHttpContext(),
(stream, encoding) => new HttpResponseStreamWriter(stream, encoding),
typeof(string),
output
) {
ContentType = "application/json"
};
// Act
var filter = new XssJsonOutputFormatter();
await filter.WriteResponseBodyAsync(context, Encoding.UTF8);
using var reader = new StreamReader(context.HttpContext.Response.Body);
var json = await reader.ReadToEndAsync();
var wanted = $"{XssJsonOutputFormatter.XssPrefix}{output}";
// Assert
Assert.Equal(wanted, json);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.