Beispiel #1
0
        public void CardController_PostCard_Should_Correct()
        {
            //arrange
            var testCard = new CardAddUICommand
            {
                Name     = "test",
                CardType = CardType.kungFu
            };

            cardServiceMock.Setup(x => x.Add(testCard))
            .Returns(Task.CompletedTask);
            cardController.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    Request = { Path = new PathString("/mocked") }
                }
            };

            //act
            var result = cardController.PostCard(testCard).Result as CreatedResult;

            Assert.IsNotNull(result);
            Assert.AreEqual("/mocked", result.Location);
        }
        public async Task PostCard(CardAddUICommand command)
        {
            var client = _clientFactory.CreateClient("spring.festival.card.api");

            var cardJson = new FormUrlEncodedContent(command.GetKeyValuePairs());

            using var httpResponse = await client.PostAsync($"/api/cards", cardJson);

            httpResponse.EnsureSuccessStatusCode();
        }
        public async Task Add(CardAddUICommand command)
        {
            var cardForAdd = _mapper.Map <Entity.Card>(command);

            var card = await _cardRepository.Get(cardForAdd.Id);

            if (card != null)
            {
                throw new Exception("card has exist!");
            }

            await _cardRepository.Add(cardForAdd);
        }
Beispiel #4
0
        public async Task <ActionResult> PostCard(CardAddUICommand command)
        {
            if (!ModelState.IsValid)
            {
                var errorMessages = new List <KeyValuePair <string, string> >();
                foreach (var key in ModelState.Keys)
                {
                    errorMessages.AddRange(ModelState[key].Errors
                                           .Select(error => new KeyValuePair <string, string>(key, error.ErrorMessage)));
                }

                return(BadRequest(errorMessages));
            }

            await _cardService.Add(command);

            return(Created(new Uri($"{Request.Path}", UriKind.Relative), null));
        }
Beispiel #5
0
        public void CardController_PostCard_Should_Return_BadRequest_When_ModelInValid()
        {
            //arrange
            var testCard = new CardAddUICommand
            {
                Name     = "test",
                CardType = CardType.kungFu
            };

            cardController.ControllerContext = new ControllerContext();
            cardController.ControllerContext.ModelState.AddModelError("default", "mocked");

            //act
            var result = cardController.PostCard(testCard).Result as BadRequestObjectResult;

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Value);
        }