public async Task <IActionResult> Get() { var locationq = await _nikoClient.GetLocations(); if (locationq.Data.IsError) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } return(Ok(locationq.Data.Locations)); }
public async Task Get_Locations_Error() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, responsemodel) = GetJson <NikoResponse <IReadOnlyList <ILocation> > >(@"Json\GetLocations_Error.json", true, new LocationsConverter()); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Callback <byte[], int, int>((b, o, l) => { jsonbytes.CopyTo(b, o); }) .Returns(() => (tcs = new TaskCompletionSource <int>()).Task); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); var responsetask = client.GetLocations(); tcs.SetResult(jsonbytes.Length); var response = await responsetask; response.Should().NotBeNull(); response.Command.Should().Be(response.Command); response.Data.Should().BeNull(); response.IsError.Should().BeTrue(); response.Error.Should().Be(responsemodel.Error); }
public async Task Get_Locations_Exception() { var tcpclientmock = new Mock <ITcpClient>(); // ReSharper disable once AccessToModifiedClosure tcpclientmock.SetupGet(c => c.IsConnected).Returns(() => false); tcpclientmock.Setup(c => c.ReadAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Returns(() => new TaskCompletionSource <int>().Task); tcpclientmock.Setup(c => c.WriteAsync(It.IsAny <byte[]>(), It.IsAny <int>(), It.IsAny <int>())) .Throws <ArgumentNullException>(); var client = new NikoClient(tcpclientmock.Object); client.StartClient(); Func <Task> act = async() => await client.GetLocations(); await act.Should().ThrowAsync <ArgumentNullException>(); }