次のようなMVCアクションがあるとします。
public ActionResult CustomerRecord(customerId)
{
if (_currentUser.CanViewCustomer(customerId))
return View();
else
{
// user has tried to access an unauthorised record,
// should not be here!
_logger.Log(new SecurityException());
return View("UnauthorizedAccess");
}
}
不正アクセスの試みのケースをテストするには、いくつのテスト方法が必要ですか?
つまり、単一のテストを作成しますか?
CustomerRecord_WithUnauthorizedUser_LogsExceptionAndReturnsUnauthorizedView
または私は2つのテストを書きますか?
CustomerRecord_WithUnauthorizedUser_LogsException
CustomerRecord_WithUnauthorizedUser_ReturnsUnauthorizedView
問題は、技術的にはコントローラーがSRPに違反していることだと思いますが、それ自体が問題であるとは考えていません(同意しない場合は修正してください)。それがテストメソッドにどのようにマッピングされるかはわかりません。メソッドの責任ごとに1つのテストですか、それともメソッドを通る単一のルートごとに1つのテストですか?