【发布时间】:2016-09-21 20:31:03
【问题描述】:
我有一个将数据导出到 CSV 文件的方法。
public FileContentResult Index(SearchModel search)
{
...
if (search.Action == SearchActionEnum.ExportToTSV)
{
const string fileName = "Result.txt";
const string tab = "\t";
var sb = BuildTextFile(result, tab);
return File(new UTF8Encoding().GetBytes(sb.ToString()), "text/tsv", fileName);
}
if (search.Action == SearchActionEnum.ExportToCSV)
{
const string fileName = "Result.csv";
const string comma = ",";
var sb = BuildTextFile(result, comma);
return File(new UTF8Encoding().GetBytes(sb.ToString()), "text/csv", fileName);
}
return null;
}
我的测试,在 NUnit 中:
[Test]
public void Export_To_CSV()
{
#region Arrange
...
#endregion
#region Act
var result = controller.Index(search);
#endregion
#region Assert
result.ShouldSatisfyAllConditions(
()=>result.FileDownloadName.ShouldBe("Result.csv"),
()=>result.ContentType.ShouldBe("text/csv")
);
#endregion
}
除了FileDownloadName和ContentType之外,我想查看result的内容。
看来我应该看看result.FileContents,但它是byte[]。
如何获取result 作为文本字符串?
每次我运行测试时,我的结果是否以 CSV 文件的形式保存在解决方案中的某个位置?
【问题讨论】:
标签: c# asp.net-mvc unit-testing nunit