■ ファイルの存在チェックに関するテスト
ファイルが存在しないテストをするために
* テスト前に、以下のようなメソッドを実行してからテストするファイル削除
// ファイルを消す
public void DeleteFileIfExists(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
ファイルが存在するテストをするために
* テスト前に、以下のようなメソッドを実行してからテストするファイル作成
// 空ファイル作成
public void CreateEmptyFileIfNotExists(string filePath)
{
if (!File.Exists(filePath))
{
using(File.Create(path))
{
}
}
}
参考文献 https://tsucchi.hatenadiary.com/entry/20080426/1209224485
■ ファイルエラーを起こさせるテスト
ファイルロックを発生
// 擬似的にファイルロックを発生させる
using (System.IO.FileStream fileStream = new System.IO.FileStream(
filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
{
// ここにメソッド実行する
Assert.IsFalse(instance.FileAccess(filePath));
}
参考文献 https://dobon.net/vb/dotnet/file/fileshare.html
関連記事
MS-Test
MS-Test あれこれhttps://blogs.yahoo.co.jp/dk521123/18458247.html
private に関するテスト
https://blogs.yahoo.co.jp/dk521123/38022249.html