コード例 #1
0
        public async Task Controller_Add_ToDo_Test()
        {
            var mockRepo = new Mock <IToDoElementRepository>();

            var myProfile     = new AutomapperProfile();
            var configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));


            var temp = new ToDoElementAdd()
            {
                Priority = 1, Tittle = "test"
            };


            mockRepo.Setup(x => x.CreateElementAsync(It.IsAny <ToDoElement>())).ReturnsAsync(new ToDoElement {
                Tittle = temp.Tittle, Priority = temp.Priority
            });

            var controller = new ToDoController(mockRepo.Object, configuration.CreateMapper());

            IActionResult result = (await controller.Post(temp));

            Assert.IsType <OkObjectResult>(result);
            var objectResponse = result as OkObjectResult;

            Assert.Equal(200, objectResponse.StatusCode);
            Assert.Equal(typeof(ToDoElementDTO), objectResponse.Value.GetType());

            ToDoElementDTO resultCast = objectResponse.Value as ToDoElementDTO;

            Assert.Equal(temp.Tittle, resultCast.Tittle);
            Assert.Equal(temp.Priority, resultCast.Priority);
        }
コード例 #2
0
        public async Task <IActionResult> Post([FromBody] ToDoElementAdd state)
        {
            if (state.Priority >= 1 && state.Priority <= 10)
            {
                if (string.IsNullOrEmpty(state.Tittle))
                {
                    return(BadRequest("TITTLE_IS_REQUIRED"));
                }
                else
                {
                    var itemToADD   = _mapper.Map <ToDoElement>(state);
                    var newToDoElem = await _repo.CreateElementAsync(itemToADD);

                    var mappedItem = _mapper.Map <ToDoElementDTO>(newToDoElem);
                    return(Ok(mappedItem));
                }
            }
            else
            {
                return(BadRequest("PRIORITY_INCORECT_RANGE"));
            }
        }