Wanted to Unit Test a method in the following Class
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()
}
}
Note:
- Method Name = AuthoriseDeviceProfile returns void
- The method checks userSentMakeModel against the one stored in the db match
- If it matches – it simply returns (ie does not change any state)
How will we unit test this method?
- Have mocked the Repo
- Have covered scenario of “THROWS EXCEPTION”
- Question is how to unit test the scenario of ALL WENT WELL ie user;s makeModel matched with repository;s makeModel
Any design suggestions to make this testable is most welcome
Thanks in advance.
Since your method returns void, it probably has some side-effect that you can test/assert on.
In your case, an option would be to provide a mock instance of
IDeviceAuthorisationRepositioryService. You can then check if a call toUpdatePhoneStatusToActivehas happened. Here is a solution using Moq: