public void CloudReceiverIgnoresOtherExceptionsWhenReceivingMessages() { //Arrange var messageConverter = new Mock <IMessageConverter <Client.Message> >(); messageConverter.Setup(p => p.ToMessage(It.IsAny <Client.Message>())).Returns <IMessage>(null); var messageConverterProvider = new Mock <IMessageConverterProvider>(); messageConverterProvider.Setup(p => p.Get <Client.Message>()).Returns(messageConverter.Object); var identity = Mock.Of <IIdentity>(i => i.Id == "device1"); var deviceClient = new Mock <IClient>(); deviceClient.Setup(p => p.ReceiveAsync(It.IsAny <TimeSpan>())).ReturnsAsync(new Client.Message(new byte[0])); var cloudProxy = new CloudProxy(deviceClient.Object, messageConverterProvider.Object, identity.Id, (id, s) => { }); var cloudListener = new Mock <ICloudListener>(); var cloudReceiver = new CloudProxy.CloudReceiver(cloudProxy, cloudListener.Object); cloudListener.Setup(p => p.ProcessMessageAsync(It.IsAny <IMessage>())) .Callback(() => cloudReceiver.CloseAsync()) .Throws(new Exception()); //Act cloudReceiver.StartListening(); //Exception expected to be handled and not thrown. //Assert cloudListener.Verify(m => m.ProcessMessageAsync(null)); }
public void CloudReceiverCanProcessAMessage() { //Arrange var sampleMessage = Mock.Of <IMessage>(); var messageConverter = new Mock <IMessageConverter <Client.Message> >(); messageConverter.Setup(p => p.ToMessage(It.IsAny <Client.Message>())).Returns(sampleMessage); var messageConverterProvider = new Mock <IMessageConverterProvider>(); messageConverterProvider.Setup(p => p.Get <Client.Message>()).Returns(messageConverter.Object); var identity = Mock.Of <IIdentity>(i => i.Id == "device1"); var deviceClient = new Mock <IClient>(); deviceClient.Setup(p => p.ReceiveAsync(It.IsAny <TimeSpan>())).ReturnsAsync(new Client.Message(new byte[0])); var cloudProxy = new CloudProxy(deviceClient.Object, messageConverterProvider.Object, identity.Id, (id, s) => { }); var cloudListener = new Mock <ICloudListener>(); var cloudReceiver = new CloudProxy.CloudReceiver(cloudProxy, cloudListener.Object); cloudListener.Setup(p => p.ProcessMessageAsync(It.IsAny <IMessage>())) .Returns(Task.CompletedTask) .Callback(() => cloudReceiver.CloseAsync()); //Act cloudReceiver.StartListening(); //Assert cloudListener.Verify(m => m.ProcessMessageAsync(sampleMessage)); }
public void CloudReceiverDisconnectsWhenItReceivesUnauthorizedException() { //Arrange var messageConverterProvider = Mock.Of <IMessageConverterProvider>(); var deviceClient = new Mock <IClient>(); deviceClient.Setup(p => p.ReceiveAsync(It.IsAny <TimeSpan>())) .Throws(new UnauthorizedException("")); var cloudProxy = new CloudProxy(deviceClient.Object, messageConverterProvider, "device1", (id, s) => { }); var cloudListener = Mock.Of <ICloudListener>(); var cloudReceiver = new CloudProxy.CloudReceiver(cloudProxy, cloudListener); //Act cloudReceiver.StartListening(); //Exception expected to be handled and not thrown. //Assert //The verification is implicit, just making sure the exception is handled. }