public async Task ExecuteCommandWithCommandResponseWithResultGeneratesOkResponse() { SimpleCommandCommandResponseResult command = new SimpleCommandCommandResponseResult(); Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>(); dispatcher.Setup(x => x.DispatchAsync(command, It.IsAny <CancellationToken>())) .ReturnsAsync(new CommandResult <CommandResponse <bool> >(CommandResponse <bool> .Ok(true), false)); TestSubjectController controller = new TestSubjectController(dispatcher.Object); IActionResult result = await controller.ExecuteCommandProxy(command); OkObjectResult castResult = (OkObjectResult)result; Assert.Equal(200, castResult.StatusCode); }
public async Task ExecuteCommandWithCommandResponseWithResultThrownExceptionGeneratesBadResponse() { SimpleCommandCommandResponseResult command = new SimpleCommandCommandResponseResult(); Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>(); dispatcher.Setup(x => x.DispatchAsync(command, It.IsAny <CancellationToken>())) .Throws(new DispatcherException("An error occurred", new Exception())); TestSubjectController controller = new TestSubjectController(dispatcher.Object); IActionResult result = await controller.ExecuteCommandProxy(command); BadRequestObjectResult castResult = (BadRequestObjectResult)result; Assert.Equal(400, castResult.StatusCode); Assert.Equal("An error occurred", ((string[])((SerializableError)castResult.Value)[""])[0]); }
public async Task ExecuteCommandWithCommandResponseWithResultGeneratesBadResponse() { SimpleCommandCommandResponseResult command = new SimpleCommandCommandResponseResult(); Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>(); dispatcher.Setup(x => x.DispatchAsync(command, It.IsAny <CancellationToken>())) .ReturnsAsync(new CommandResult <CommandResponse <bool> >(CommandResponse <bool> .WithError("went wrong"), false)); TestSubjectController controller = new TestSubjectController(dispatcher.Object); IActionResult result = await controller.ExecuteCommandProxy(command); BadRequestObjectResult castResult = (BadRequestObjectResult)result; Assert.Equal(400, castResult.StatusCode); Assert.Equal("went wrong", ((string[])((SerializableError)castResult.Value)[""])[0]); }
public async Task LogBeforeAndAfterSuccessfulCommand() { Mock <IFrameworkCommandDispatcher> frameworkCommandDispatcher = new Mock <IFrameworkCommandDispatcher>(); TestLogger logger = new TestLogger(); Mock <IMetricCollectorFactory> metricCollectorFactory = new Mock <IMetricCollectorFactory>(); Mock <IMetricCollector> metricCollector = new Mock <IMetricCollector>(); metricCollectorFactory.Setup(x => x.Create(It.IsAny <Type>())).Returns(metricCollector.Object); SimpleCommandCommandResponseResult command = new SimpleCommandCommandResponseResult(); LoggingCommandDispatcher testSubject = new LoggingCommandDispatcher( frameworkCommandDispatcher.Object, logger, metricCollectorFactory.Object ); await testSubject.DispatchAsync(command); Assert.Equal("Executing command SimpleCommandCommandResponseResult", logger.Messages[0]); Assert.Equal("Successfully executed command SimpleCommandCommandResponseResult", logger.Messages[1]); frameworkCommandDispatcher.Verify(x => x.DispatchAsync(command, It.IsAny <CancellationToken>())); }
public async Task LogAndReturnErrorResult() { Mock <IFrameworkCommandDispatcher> frameworkCommandDispatcher = new Mock <IFrameworkCommandDispatcher>(); TestLogger logger = new TestLogger(); Mock <IMetricCollectorFactory> metricCollectorFactory = new Mock <IMetricCollectorFactory>(); Mock <IMetricCollector> metricCollector = new Mock <IMetricCollector>(); metricCollectorFactory.Setup(x => x.Create(It.IsAny <Type>())).Returns(metricCollector.Object); SimpleCommandCommandResponseResult command = new SimpleCommandCommandResponseResult(); frameworkCommandDispatcher.Setup(x => x.DispatchAsync(command, It.IsAny <CancellationToken>())) .Throws(new Exception()); LoggingCommandDispatcher testSubject = new LoggingCommandDispatcher( frameworkCommandDispatcher.Object, logger, metricCollectorFactory.Object ); CommandResult <CommandResponse <bool> > result; await Assert.ThrowsAsync <DispatcherException>(async() => result = await testSubject.DispatchAsync(command)); Assert.Equal("Executing command SimpleCommandCommandResponseResult", logger.Messages[0]); Assert.Equal("Error executing command SimpleCommandCommandResponseResult", logger.Messages[1]); }