public async Task <ConnectionResponse> ReadAsync() { var result = await _client.GetAsync(_uriToCheck); if (result is null) { return(ConnectionResponse.Error( new ResponseError($"{nameof(NetworkStatusConnection)}: result was null!"))); } if (!result.IsSuccessStatusCode) { return(ConnectionResponse.Error( new ResponseError(SensorConnectionConstants.CONNECTION_FAILED_READING))); } if (result.Content is null) { return(ConnectionResponse.Valid(string.Empty)); } var content = await result.Content?.ReadAsByteArrayAsync(); return(ConnectionResponse.Valid(Encoding.ASCII.GetString(content), content)); }
public async Task SensorWithValidConnectionShouldReturnValidReading() { var networkConnectionMock = new Mock <ISensorConnection>(); networkConnectionMock.Setup(connection => connection.ReadAsync()) .ReturnsAsync(ConnectionResponse.Valid("Message!")); var sensor = new ConnectionSensor(networkConnectionMock.Object); var reading = await sensor.ReadAsync(); Assert.True(reading.IsValid); }