CSV を解析する C# アプリケーションの単体テストを行っています。try/catch ブロックを強制的に失敗させることはできないため、コード カバレッジは 94% です。Nuget http://joshclose.github.io/CsvHelperの CsvHelper を使用しています。
public void ParseCsv([FromBody] string csvText)
{
var parseCsv = new XsvData(new[] { "\t", "," });
try
{
using (var reader = new XsvReader(new StringReader(csvText)))
{
parseCsv.Read(reader, headerExists: true);
}
}
catch (Exception)
{
var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Unable to read CSV."),
ReasonPhrase = "Invalid CSV"
};
throw new HttpResponseException(response);
}
}
考えられる最もあいまいな文字列を渡そうとしましたが、これを通過し、後で関数でエラーが発生します..
[TestMethod]
//[ExpectedException(typeof(HttpResponseException))]
public void TestUploadCsv_UploadingCsvNonCsv()
{
const string csvText = "fhfhk@- jhjfh@ajh- fjkqeqir%hjewq@hf- ujewqh$phfuw \n hfwu- ihfq&if*u@q- afuhwu- fhiue@wfhiuewhiuf";
var context = GetMyFakeEntityDatabase();
var controller = new MyController(context);
controller.ParseCsv(csvText);
}
try/catch ブロックの後に、すべてのヘッダーが存在することを強制するセクションがあり、そこで失敗しますが、この例では、読み取り中に失敗するはずです。単体テストを強制的に失敗させるにはどうすればよいですか? どんな助けでも大歓迎です!前もって感謝します。