Пример #1
0
        public async Task PostTodo_Should_ReturnOk()
        {
            // Arrange
            var adapterMock = new Mock <ITodoAdapter>();
            var loggerMock  = new Mock <ILogger <TodoController> >();

            var testTodo = new TodoViewModel
            {
                TodoId = 3,
                Title  = "test todo 3",
                Points = new List <TodoPointViewModel> {
                }
            };

            adapterMock.Setup(x => x.AddTodo(testTodo)).Returns(Task.FromResult(testTodo));
            var controller = new TodoController(adapterMock.Object, loggerMock.Object);

            // Act
            var result = await controller.PostTodo(testTodo);

            // Assert
            var okResult    = Assert.IsAssignableFrom <OkObjectResult>(result);
            var returnValue = Assert.IsAssignableFrom <TodoViewModel>(okResult.Value);

            Assert.Equal(testTodo.Title, returnValue.Title);
        }
Пример #2
0
        public async Task PostTodo_Should_ReturnBadRequest()
        {
            // Arrange
            var adapterMock = new Mock <ITodoAdapter>();
            var loggerMock  = new Mock <ILogger <TodoController> >();

            var controller = new TodoController(adapterMock.Object, loggerMock.Object);

            // Act
            var result = await controller.PostTodo(null);

            // Assert
            var actionResult = Assert.IsAssignableFrom <ActionResult>(result);
            var returnValue  = Assert.IsAssignableFrom <BadRequestObjectResult>(actionResult);

            Assert.Equal("Todo object is null", returnValue.Value);
        }
        public async Task PostTodo_CallsAddAsync()
        {
            // Arrange
            var todoServiceMock = new Mock <ITodoService>();
            var todoController  = new TodoController(todoServiceMock.Object);

            var todo = GetTodo();

            todoServiceMock
            .Setup(s => s.AddAsync(It.IsAny <Todo>()))
            .Returns(Task.CompletedTask);

            // Act
            var result = await todoController.PostTodo(todo);

            // Assert
            todoServiceMock.Verify(s =>
                                   s.AddAsync(It.Is <Todo>(t =>
                                                           t.IsComplete == todo.IsComplete &&
                                                           t.Name == todo.Name
                                                           )), Times.Once());
            Assert.IsAssignableFrom <CreatedAtActionResult>(result.Result);
            Assert.Equal(1, (result.Result as CreatedAtActionResult).RouteValues["id"]);
        }