Пример #1
0
        public async Task ExecuteGenericCommandWithCommandResponseWithNoResultGeneratesOkResponse()
        {
            Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>();

            dispatcher.Setup(x => x.DispatchAsync(It.IsAny <SimpleCommandCommandResponse>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(new CommandResult <CommandResponse>(CommandResponse.Ok(), false));
            TestSubjectController controller = new TestSubjectController(dispatcher.Object);

            IActionResult result = await controller.ExecuteCommandProxy <SimpleCommandCommandResponse>();

            OkResult castResult = (OkResult)result;

            Assert.Equal(200, castResult.StatusCode);
        }
Пример #2
0
        public async Task ExecuteCommandWithResultAndGenerateOkResponse()
        {
            SimpleCommandWithResult   command    = new SimpleCommandWithResult();
            Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>();

            dispatcher.Setup(x => x.DispatchAsync(command, It.IsAny <CancellationToken>())).ReturnsAsync(new CommandResult <bool>(true, false));
            TestSubjectController controller = new TestSubjectController(dispatcher.Object);

            IActionResult result = await controller.ExecuteCommandProxy(command);

            OkObjectResult castResult = (OkObjectResult)result;

            Assert.True((bool)castResult.Value);
            Assert.Equal(200, castResult.StatusCode);
        }
Пример #3
0
        public async Task ExecuteGenericCommandWithCommandResponseWithResultThrownExceptionGeneratesBadResponse()
        {
            Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>();

            dispatcher.Setup(x => x.DispatchAsync(It.IsAny <SimpleCommandCommandResponseResult>(), It.IsAny <CancellationToken>()))
            .Throws(new DispatcherException("An error occurred", new Exception()));
            TestSubjectController controller = new TestSubjectController(dispatcher.Object);

            IActionResult result = await controller.ExecuteCommandProxy <SimpleCommandCommandResponseResult, bool>();

            BadRequestObjectResult castResult = (BadRequestObjectResult)result;

            Assert.Equal(400, castResult.StatusCode);
            Assert.Equal("An error occurred", ((string[])((SerializableError)castResult.Value)[""])[0]);
        }
Пример #4
0
        public async Task ExecuteGenericCommandWithCommandResponseWithResultGeneratesBadResponse()
        {
            Mock <ICommandDispatcher> dispatcher = new Mock <ICommandDispatcher>();

            dispatcher.Setup(x => x.DispatchAsync(It.IsAny <SimpleCommandCommandResponseResult>(), 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 <SimpleCommandCommandResponseResult, bool>();

            BadRequestObjectResult castResult = (BadRequestObjectResult)result;

            Assert.Equal(400, castResult.StatusCode);
            Assert.Equal("went wrong", ((string[])((SerializableError)castResult.Value)[""])[0]);
        }
Пример #5
0
        public async Task ExecuteCommandWithResultAndGenerateModelStateErrorOnDispatchErrorException()
        {
            SimpleCommandWithResult   command    = new SimpleCommandWithResult();
            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 ExecuteCommandWithCommandResponseNoResultGeneratesBadResponse()
        {
            SimpleCommandCommandResponse command    = new SimpleCommandCommandResponse();
            Mock <ICommandDispatcher>    dispatcher = new Mock <ICommandDispatcher>();

            dispatcher.Setup(x => x.DispatchAsync(command, It.IsAny <CancellationToken>()))
            .ReturnsAsync(new CommandResult <CommandResponse>(CommandResponse.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", castResult.Value);
        }