繁体   English   中英

单元测试返回void的方法

[英]Unit Test a method that returns a void

想要单元测试以下类中的方法

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匹配中存储的那个
  • 如果它匹配 - 它只是返回(即不改变任何状态)

我们如何对这种方法进行单元测试?

  • 嘲笑了回购
  • 涵盖了“THROWS EXCEPTION”的场景
  • 问题是如何对所有WENT WELL即用户的场景进行单元测试; s makeModel与repository匹配; s makeModel

任何设计建议,以使这个可测试是非常欢迎提前感谢。

由于您的方法返回void,因此可能会有一些可以测试/断言的副作用。

在您的情况下,一个选项是提供IDeviceAuthorisationRepositioryService的模拟实例。 然后,您可以检查是否发生了对UpdatePhoneStatusToActive的调用。 这是使用Moq的解决方案:

var mock = new Mock<IDeviceAuthorisationRepositioryService>();

var service = new DeviceAuthorisationService(mock.Object);
service.UpdateDeviceStatusToActive(....);

mock.Verify(x => service.UpdatePhoneStatusToActive(), Times.Never());

如果一个方法是无效的,那么它应该有一些可观察到的副作用 - 否则它是毫无意义的。 因此,不测试返回值,而是测试副作用。 在这种情况下,看起来可能是在哪些情况下抛出异常。

(这里,“抛出异常”被视为副作用;你当然也可以认为它是一种隐含的返回值......)

注入一个模拟的存储库。 测试是否调用存储库上的某些方法。

您可以在单元测试中设置异常预期。 在nUnit中它看起来像这样:

[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void TestAuthoriseFail()
{
    // do something that should make the tested method throw the exception
}

即使你的方法返回void,它也必须做一些对你有用的事情(否则它将是一个毫无意义的方法)。

从你的代码中,我猜测AuthoriseDeviceProfile方法正在做的有两种“有用”的东西:

  • 调用GetSelectedPhoneDetails的方法IDeviceAuthorisationRepositiory
  • 根据某些条件抛出各种例外

因此,要对方法进行单元测试,您应该做两件与此相对应的事情:

  • 注入模拟IDeviceAuthorisationRepositiory并让它记录和/或断言是否GetSelectedPhoneDetails
  • 运用测试方法来引发各种异常,并在抛出它们时捕获它们以验证:
    • 事实上抛出了一个例外
    • 抛出的异常是每个方案的合适异常
  [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);
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM