コード例 #1
0
        public void CreateListTest_EmptyTitleReturnBadRequest()
        {
            #region Arrange
            CreateListDTO createListDTO = new CreateListDTO
            {
                Title = ""
            };

            TodoList existTodoList = null;

            TodoList newTodoList = new TodoList
            {
                Id     = 1,
                UserId = 1,
                Title  = "List1"
            };

            User user = new User(1, "Name1", "Email1", "Pass1");
            Extensions.Extensions.IsUnitTest = true;

            model = new Mock <IRepository>();

            controller = new TodoListsController(model.Object);
            #endregion

            // Act
            var result = controller.CreateList(createListDTO);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result.Result);
        }
コード例 #2
0
        public void CreateListTest_ReturnExistToDoList()
        {
            #region Arrange
            CreateListDTO createListDTO = new CreateListDTO
            {
                Title = "List1"
            };

            TodoList existTodoList = new TodoList
            {
                Id     = 1,
                UserId = 1,
                Title  = "List1"
            };

            User user = new User(1, "Name1", "Email1", "Pass1");
            Extensions.Extensions.IsUnitTest = true;

            model = new Mock <IRepository>();
            model.Setup(repo => repo.GetToDoLists()).Returns(GetTodoListsTest());
            model.Setup(repo => repo.GetTodoListByTitleAndUserId(
                            createListDTO.Title, user.Id)).Returns(Task.FromResult(existTodoList));
            model.Setup(repo => repo.GetUserById(user.Id)).Returns(Task.FromResult(user));

            controller = new TodoListsController(model.Object);
            #endregion

            // Act
            var result = controller.CreateList(createListDTO);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result.Result);
        }
コード例 #3
0
        public async Task <IActionResult> CreateList([FromBody] CreateListDTO createListDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Error: Model state is not valid."));
            }

            if (createListDTO == null ||
                createListDTO.Title == String.Empty)
            {
                return(BadRequest("Error: Title is empty."));
            }

            string title = createListDTO.Title;

            if (title == null || title.Length == 0)
            {
                return(BadRequest("Error: Title cannot to be empty."));
            }

            var userId = this.User.GetUserId();

            User user = await _context.GetUserById(userId);

            if (user == null)
            {
                return(NotFound("Error: User not found."));
            }

            TodoList existToDoList = await _context
                                     .GetTodoListByTitleAndUserId(title, userId);

            if (existToDoList != null)
            {
                return(BadRequest("Error: This Todo List already exist."));
            }

            TodoList todoList = new TodoList(userId, title);

            _context.AddTodoList(todoList);

            if (Extensions.Extensions.IsUnitTest)
            {
                return(Created("localhost", todoList));
            }

            string webRootPath    = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}";
            string objectLocation = webRootPath + "/" + "api/todolists/" + todoList.Id.ToString();

            return(Created(objectLocation, todoList));
        }
コード例 #4
0
        private async Task <TodoList> CreateToDoListTest(string title)
        {
            // Arrange
            CreateListDTO createListDTO = new CreateListDTO
            {
                Title = title,
            };
            var content       = JsonConvert.SerializeObject(createListDTO);
            var stringContent = new StringContent(content, Encoding.UTF8, "application/json");

            // Act
            var response = await _client.PostAsync("/api/TodoLists/", stringContent);

            // Assert
            var responseString = await response.Content.ReadAsStringAsync();

            var todoList = JsonConvert.DeserializeObject <TodoList>(responseString);

            Assert.NotNull(todoList);
            return(todoList);
        }