public void StartKeepAliveException() { var fixture = new Fixture(); string error = fixture.Create <string>(); string serverUri = fixture.Create <string>(); long leaseId = fixture.Create <long>(); CancellationToken cancellationToken = fixture.Create <CancellationToken>(); Mock <IEtcdCompoundClient> clientMock = new Mock <IEtcdCompoundClient>(); clientMock .Setup(p => p.LeaseKeepAlive(It.IsAny <long>(), It.IsAny <CancellationToken>())) .Callback <long, CancellationToken>((l, c) => throw new IOException(error)); Mock <IEtcdCompoundClientFactory> etcdCompoundClientFactoryMock = new Mock <IEtcdCompoundClientFactory>(); etcdCompoundClientFactoryMock.Setup(p => p.CreateClient(It.IsAny <string>())).Returns(clientMock.Object); EtcdWrapper wrapper = new EtcdWrapper(etcdCompoundClientFactoryMock.Object); Task returnedTask = wrapper.StartKeepAlive(serverUri, leaseId, cancellationToken); etcdCompoundClientFactoryMock.Verify(p => p.CreateClient(serverUri), Times.Once); Assert.IsNull(returnedTask); clientMock.Verify(p => p.LeaseKeepAlive(leaseId, cancellationToken), Times.Once); }
public void StartKeepAliveCreatesTask() { var fixture = new Fixture(); string serverUri = fixture.Create <string>(); long leaseId = fixture.Create <long>(); CancellationToken cancellationToken = fixture.Create <CancellationToken>(); Task task = fixture.Create <Task>(); Mock <IEtcdCompoundClient> clientMock = new Mock <IEtcdCompoundClient>(); clientMock.Setup(p => p.LeaseKeepAlive(It.IsAny <long>(), It.IsAny <CancellationToken>())).Returns(task); Mock <IEtcdCompoundClientFactory> etcdCompoundClientFactoryMock = new Mock <IEtcdCompoundClientFactory>(); etcdCompoundClientFactoryMock.Setup(p => p.CreateClient(It.IsAny <string>())).Returns(clientMock.Object); EtcdWrapper wrapper = new EtcdWrapper(etcdCompoundClientFactoryMock.Object); Task returnedTask = wrapper.StartKeepAlive(serverUri, leaseId, cancellationToken); etcdCompoundClientFactoryMock.Verify(p => p.CreateClient(serverUri), Times.Once); Assert.AreSame(task, returnedTask); clientMock.Verify(p => p.LeaseKeepAlive(leaseId, cancellationToken), Times.Once); }