假设我有一个 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
还是我写两个测试:
CustomerRecord_WithUnauthorizedUser_LogsException
CustomerRecord_WithUnauthorizedUser_ReturnsUnauthorizedView
我想问题是从技术上讲控制器违反了 SRP,但我不认为这本身就是一个问题(如果您不同意,请纠正我)。我只是不确定这如何映射到测试方法。每个方法的责任一个测试,还是通过该方法的每个单一路径一个测试?