【发布时间】:2012-12-11 12:51:44
【问题描述】:
想要对以下类中的方法进行单元测试
public class DeviceAuthorisationService : IDeviceAuthorisationService
{
private DeviceDetailsDTO deviceDetailsDTO = null;
private IDeviceAuthorisationRepositiory deviceAuthorisationRepositiory;
public DeviceAuthorisationService(IDeviceAuthorisationRepositioryService paramDeviceAuthorisationRepository)
{
deviceAuthorisationRepositiory = paramDeviceAuthorisationRepository;
}
public void AuthoriseDeviceProfile(long paramUserID, string paramClientMakeModel)
{
if (deviceDetailsDTO == null)
GetCellPhoneDetails(userID);
if (deviceDetailsDTO.IsDeviceSelected == false)
throw new SomeCustomExceptionA();
if (deviceDetailsDTO.CellPhoneMakeModel.ToLower() != paramClientMakeModel.ToLower())
throw new SomeCustomExceptionB;
}
public void UpdateDeviceStatusToActive(long userID)
{
if (deviceDetailsDTO == null)
throw new InvalidOperationException("UnAuthorised Device Profile Found Exception");
if (deviceDetailsDTO.PhoneStatus != (short)Status.Active.GetHashCode())
deviceAuthorisationRepositiory.UpdatePhoneStatusToActive(deviceDetailsDTO.DeviceID);
}
private void GetCellPhoneDetails(long userID)
{
deviceDetailsDTO = deviceAuthorisationRepositiory.GetSelectedPhoneDetails(userID);
if (deviceDetailsDTO == null)
throw new SomeCustomException()
}
}
注意:
- 方法名称 = AuthoriseDeviceProfile 返回 void
- 该方法检查 userSentMakeModel 与存储在 db 匹配中的那个
- 如果匹配 - 它只是返回(即不改变任何状态)
我们将如何对这个方法进行单元测试?
- 嘲笑了回购
- 已涵盖“抛出异常”的场景
- 问题是如何对 ALL WENT WELL 的场景进行单元测试,即 user;s makeModel 与 repository;s makeModel 匹配
欢迎提出任何使其可测试的设计建议 提前致谢。
【问题讨论】:
标签: c# unit-testing dependency-injection