unit testing返回void的方法
想要unit testing以下类中的方法
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匹配中存储的那个
- 如果它匹配 – 它只是返回(即不改变任何状态)
我们如何对这种方法进行unit testing?
- 嘲笑了回购
- 涵盖了“THROWS EXCEPTION”的场景
- 问题是如何对所有WENT WELL即用户的场景进行unit testing; s makeModel与repository匹配; s makeModel
任何设计建议,使这个可测试是最受欢迎的,在此先感谢。
由于您的方法返回void,因此它可能具有一些您可以测试/断言的副作用。
在您的情况下,一个选项是提供IDeviceAuthorisationRepositioryService
的模拟实例。 然后,您可以检查是否发生了对UpdatePhoneStatusToActive
的调用。 这是使用Moq的解决方案:
var mock = new Mock(); var service = new DeviceAuthorisationService(mock.Object); service.UpdateDeviceStatusToActive(....); mock.Verify(x => service.UpdatePhoneStatusToActive(), Times.Never());
如果一个方法是无效的,那么它应该有一些可观察到的副作用 – 否则它是没有意义的。 因此,不测试返回值,而是测试副作用。 在这种情况下,看起来可能是在哪些情况下抛出exception。
(这里,“抛出exception”被视为副作用;你当然也可以认为它是一种隐含的返回值……)
注入一个模拟的存储库。 测试是否调用存储库中的某些方法。
您可以在unit testing中设置exception预期。 在nUnit中它看起来像这样:
[Test] [ExpectedException(typeof(InvalidOperationException))] public void TestAuthoriseFail() { // do something that should make the tested method throw the exception }
即使你的方法返回void,它也必须做一些对你有用的事情(否则它将是一个毫无意义的方法)。
从你的代码中,我猜测AuthoriseDeviceProfile
方法正在做的有两种“有用”的东西:
- 在
GetSelectedPhoneDetails
上调用GetSelectedPhoneDetails
方法 - 根据某些条件抛出各种例外
因此,要对方法进行unit testing,您应该做两件与此相对应的事情:
- 注入模拟
IDeviceAuthorisationRepositiory
并让它记录和/或断言是否GetSelectedPhoneDetails
- 运用测试方法来引发各种exception,并在抛出它们时捕获它们以validation:
- 事实上抛出了一个例外
- 抛出的exception是每个方案的合适exception
[TestMethod] public void AuthoriseDeviceProfileTest() { long paramUserID=1, string paramClientMakeModel=test"; IDeviceAuthorisationService DeviceAuthorisationService= new DeviceAuthorisationService(); try { DeviceAuthorisationService.AuthoriseDeviceProfile(paramUserID, paramClientMakeModel); Assert.IsNull(paramUserID); } catch (Exception e) { Assert.AreNotEqual("Exception of type was thrown", e.Message); } } }