public async Task <IActionResult> Get() { var actions = await _nikoClient.GetActions(); if (actions.Data.IsError) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } return(Ok(actions.Data.Actions)); }
public async Task Get_Actions_Error() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, responsemodel) = GetJson <NikoResponse <IReadOnlyList <IAction> > >(@"Json\GetActions_Error.json", true, new ActionsConverter()); // 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.GetActions(); 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_Actions_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.GetActions(); await act.Should().ThrowAsync <ArgumentNullException>(); }