public async Task ExecuteCommand_Error() { TaskCompletionSource <int> tcs = null; var tcpclientmock = new Mock <ITcpClient>(); var(jsonbytes, responsemodel) = GetJson <NikoResponse <ErrorImp> >(@"Json\ExecuteCommand_Error.json", true, new BaseResponseConverter()); // 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.ExecuteCommand(31, 100); tcs.SetResult(jsonbytes.Length); var response = await responsetask; response.Should().NotBeNull(); response.Command.Should().Be(response.Command); response.Error.Should().Be(responsemodel.Data.Error); }
public async Task <IActionResult> Post([FromBody] Action value) { var response = await _nikoClient.ExecuteCommand(value.Id, value.Value); if (response.Data.IsError) { return(new StatusCodeResult(StatusCodes.Status500InternalServerError)); } _nikoClient.OnValueChanged += NikoClientOnOnValueChanged; return(Ok(response)); }
public async Task ExecuteCommand_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.ExecuteCommand(31, 0); await act.Should().ThrowAsync <ArgumentNullException>(); }