c# - 对使用RegistryManager的类进行单元测试 C# Azure IoTHub

标签 c# unit-testing azure mocking azure-iot-hub

我正在尝试测试使用 RegistryManager 与 IoThub 通信的类。

我面临的问题是,在创建继承自 RegistryManager 的模拟类时,我能够重写除 ExportRegistryAsync 之外的所有方法。我在覆盖下看到一条红线,如果删除覆盖语句,我在构建项目时会收到​​此错误:

Error 4 'MockObjects.MockRegistryManager' does not implement inherited abstract member 'Microsoft.Azure.Devices.RegistryManager.ExportRegistryAsync(string, string)' Tests\MockObjects\MockRegistryManager.cs 9 18

代码:

public class MockRegistryManager : RegistryManager
{
    private static List<Device> _devices;

    public MockRegistryManager()
    {
        _devices = new List<Device>();
    }

    public override Task OpenAsync()
    {
        throw new NotImplementedException();
    }


    ...


    internal override Task ExportRegistryAsync(string storageAccountConnectionString, string containerName)
    {
        throw new NotImplementedException();
    }

    internal override Task ExportRegistryAsync(string storageAccountConnectionString, string containerName, CancellationToken cancellationToken)
    {
        throw new NotImplementedException();
    }

}

是否有更好的方法来测试使用 RegistryManager 的类?

任何帮助将不胜感激。

最佳答案

给定要测试的类的当前版本

public class Registry {
    private readonly RegistryManager _registryManager;

    public Registry(RegistryManager rm) {
        _registryManager = rm;
    }

    public async Task<string> GetDeviceKey(string deviceId = null) {
        if (deviceId == null) {
            throw new Exception("Todo replace");
        }
        var device = await _registryManager.GetDeviceAsync(deviceId);
        if (device == null) {
            throw new Exception("TODO replace");
        }
        return device.Authentication.SymmetricKey.PrimaryKey;
    }
}

如果您想对此进行测试,那么 RegistryManager 将会遇到问题。您需要对要使用的服务进行抽象,以便可以模拟/伪造它们进行测试,而无需使用真实的服务。

类似的东西

public interface IRegistryManager {
    Task<Device> GetDeviceAsync(string deviceId);
}

这将允许您像这样重构您的类

public class Registry {
    private readonly IRegistryManager _registryManager;

    public Registry(IRegistryManager rm) {
        _registryManager = rm;
    }

    public async Task<string> GetDeviceKey(string deviceId = null) {
        if (deviceId == null) {
            throw new Exception("Todo replace");
        }
        var device = await _registryManager.GetDeviceAsync(deviceId);
        if (device == null) {
            throw new Exception("TODO replace");
        }
        return device.Authentication.SymmetricKey.PrimaryKey;
    }
}

现在允许您的 Registry 类完全可测试。您会注意到,除了注册表管理器字段的类型之外,不需要更改其他任何内容。不错。

您现在可以根据需要制作一个假的 RegistryManager 或使用测试框架进行模拟。

当您需要在生产代码中进行实际调用时,您只需将实际内容包装在界面中并将其传递到您的 Registry 类中

public class ActualRegistryManager : IRegistryManager {
    private readonly RegistryManager _registryManager

    public ActualRegistryManager (RegistryManager manager) {
        _registryManager = manager;
    }

    public Task<Device> GetDeviceAsync(string deviceId) {
        return _registryManager.GetDeviceAsync(deviceId);
    }
}

这种方法的好处之一是,您现在只需向依赖类公开真正需要的功能。

使用 MoqFluentAssertions 我能够通过以下测试来模拟和测试 Registry

[TestMethod]
public async Task Registry_Should_Return_DeviceKey() {
    //Arrange
    var expectedPrimaryKey = Guid.NewGuid().ToString();
    var deviceId = Guid.NewGuid().ToString();
    var fakeDevice = new Device(deviceId) {
        Authentication = new AuthenticationMechanism {
            SymmetricKey = new SymmetricKey {
                 PrimaryKey = expectedPrimaryKey
            }
        }
    };
    var registryManagerMock = new Mock<IRegistryManager>();
    registryManagerMock.Setup(m => m.GetDeviceAsync(deviceId))
        .ReturnsAsync(fakeDevice);
    var registry = new Registry(registryManagerMock.Object);

    //Act                
    var deviceKey = await registry.GetDeviceKey(deviceId);

    //Assert
    deviceKey.Should().BeEquivalentTo(expectedPrimaryKey);
}

希望这有帮助。

关于c# - 对使用RegistryManager的类进行单元测试 C# Azure IoTHub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36647213/

相关文章:

c# - 使用表达式树设置字段值

java - 如何仅模拟函数的一部分

azure - 访问 token 给出不同的澳元?

c# - WinAPI MoveWindow 功能不适用于某些窗口

c# - 使用 SQL 中的数据在 C# 中创建 Json 数组

c# - 如何使用 Linq 检查列表中的所有元素是否为属性返回 true?

c# - 在回调模拟设置中设置 ManualResetEvent 时出错

unit-testing - 从 MSTest 迁移到 XUnit

azure - 使用 Azure 进行 Terraform

azure - 如何登录 Azure Web 作业?