给定您要测试的类的当前版本
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);
}
}
这种方法的好处之一是您现在只需要向依赖类公开您真正需要的功能。
使用Moq
并且FluentAssertions
我能够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);
}
希望这可以帮助。