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

时间:2016-04-15 12:30:12

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

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

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

  

错误4' MockObjects.MockRegistryManager'没有实现继承的抽象成员' Microsoft.Azure.Devices.RegistryManager.ExportRegistryAsync(string,string)'测试\ 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的课程?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

鉴于您要测试的课程的当前版本

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

希望这有帮助。