单元测试使用 RegistryManager C# Azure IoTHub 的类

本文关键字:Azure IoTHub 的类 RegistryManager 单元测试 | 更新日期: 2024-09-21 04:22:26

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

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

错误 4 'MockObjects.MockRegistryManager'

不实现继承的抽象成员 '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的类?

任何帮助将不胜感激。

单元测试使用 RegistryManager C# Azure IoTHub 的类

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

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);
}

希望这有帮助。