Exemplo n.º 1
0
        public void TodoController_Post_return_Internal_error()
        {
            _tasksService.Setup(x => x.IsValidTaskForInsert(It.IsAny <Task>())).Throws(new Exception());

            var response = _todoController.Post(new TaskModel());

            Assert.AreEqual(HttpStatusCode.InternalServerError, response.StatusCode);
        }
Exemplo n.º 2
0
        public async Task Respond_To_POST()
        {
            JObject payload = new JObject {
                ["title"] = "a todo"
            };

            var result = await _controller.Post(payload);

            Assert.IsInstanceOf(typeof(JObject), result);
            Assert.AreEqual("a todo", result["title"].Value <string>());
        }
Exemplo n.º 3
0
        public void Test_Add_Success()
        {
            var todo = new TodoItem()
            {
                Id = 0, Description = ""
            };

            _todosRepository.Setup(x => x.Add(null));

            var result = _todoController.Post(todo);

            Assert.IsInstanceOfType(result, typeof(CreatedResult));
        }
Exemplo n.º 4
0
        public void PostTest()
        {
            var userId      = 1;
            var todoId      = 1;
            var description = "AAA";

            var todoVM = new TodoViewModel
            {
                Id          = 1,
                Description = "AAA",
                Check       = false
            };

            // Arrange
            var mockServ = new Mock <ITodoService>();

            mockServ.Setup(s => s.CreateTodo(description, userId)).Returns(todoId);
            var controller = new TodoController(mockServ.Object);

            controller.ControllerContext = FakeContext;

            // Act
            var actualResp = controller.Post(todoVM) as OkObjectResult;
            var actual     = int.Parse(actualResp.Value.ToString());

            // Assert
            Assert.AreEqual(actual, todoId);
        }
Exemplo n.º 5
0
        public async Task Test_Post_Failure()
        {
            string ERROR_MSG = "error message";
            var    logger    = new Mock <ILogger <TodoController> >();
            var    processor = new Mock <ITodoProcessor>();

            processor.Setup(p => p.Handle(It.IsAny <TodoCommand>())).Returns <TodoCommand>(p => new TodoResponse()
            {
                IsSuccess = false, ErrorMessage = ERROR_MSG
            });

            var todoController = new TodoController(logger.Object, processor.Object);
            var response       = await todoController.Post(new TodoCommand()
            {
            });

            var okResponse = response as ObjectResult;

            Assert.IsNotNull(okResponse);
            var todoResponse = okResponse.Value as TodoResponse;

            Assert.IsNotNull(todoResponse);
            Assert.IsFalse(todoResponse.IsSuccess);
            Assert.AreEqual(todoResponse.ErrorMessage, ERROR_MSG);
        }
Exemplo n.º 6
0
        public async Task Test_Post_GetList_HappyFlow()
        {
            var logger    = new Mock <ILogger <TodoController> >();
            var processor = new Mock <ITodoProcessor>();

            processor.Setup(p => p.Handle(It.IsAny <TodoCommand>())).Returns <TodoCommand>(p => new TodoResponse()
            {
                IsSuccess = true,
                TaskList  = new List <TodoItem>()
                {
                    new TodoItem()
                    {
                        Title = "task1"
                    }
                }
            });

            var todoController = new TodoController(logger.Object, processor.Object);
            var response       = await todoController.Post(new TodoCommand()
            {
            });

            var okResponse = response as OkObjectResult;

            Assert.IsNotNull(okResponse);
            var todoResponse = okResponse.Value as TodoResponse;

            Assert.IsNotNull(todoResponse);
            Assert.IsTrue(todoResponse.IsSuccess);
            Assert.IsTrue(todoResponse.TaskList.Count == 1);
        }
        public void TodoControllerTest_AddTodo()
        {
            // Arrange
            var     mockTodoService = new Mock <ITodoService>();
            TodoDTO todoDTO         = new TodoDTO {
                Name        = "test",
                AssigneeId  = 1,
                Description = "test add todo endpoint",
                Done        = false,
                UserId      = 1
            };

            mockTodoService.Setup(x => x.CreateTodo(todoDTO))
            .ReturnsAsync(new OkResult());

            TodoController todoController = new TodoController(mockTodoService.Object);

            // Act
            Task <IActionResult> addTodoResult = todoController.Post(todoDTO);

            // Assert
            var expected = new OkResult();

            Assert.IsNotNull(addTodoResult);
            Assert.AreEqual(expected.GetType(), addTodoResult.Result.GetType());
        }
Exemplo n.º 8
0
        public void Add_InvalidObjectPassed_ReturnsBadRequest()
        {
            // Arrange
            var nameMissingItem = new CreateTodoItemDto()
            {
                Description = "Missing Name of Todo Item",
                Date        = DateTime.Now
            };

            _controller.ModelState.AddModelError("Name", "Required");

            // Act
            var badResponse = _controller.Post(nameMissingItem).Result;

            // Assert
            Assert.IsType <BadRequestObjectResult>(badResponse.Result);
        }
Exemplo n.º 9
0
        public void Post_Creates_New_Todo()
        {
            var underTest = new TodoController();

            var result = underTest.Post("New task");

            Assert.Contains("New task", result.Value);
        }
        public void Post_Saves_Todo()
        {
            var todo       = new Todo();
            var controller = new TodoController();

            controller.Post(todo);
            var result = controller.GetAll();

            Assert.That(result.Count, Is.EqualTo(2));
        }
        public void PostById_Negative_ReturnsBadRequest()
        {
            var  datarepo = new Mock <IDataRepo>();
            Note note     = new Note
            {
                NoteId = 4,
                Title  = "Testing Post"
            };

            datarepo.Setup(d => d.PostNote(note)).Returns(false);
            TodoController todoController = new TodoController(datarepo.Object);
            var            actionResult   = todoController.Post(note);

            var brObjectResult = actionResult as BadRequestObjectResult;

            Assert.NotNull(brObjectResult);
        }
Exemplo n.º 12
0
        public void Post_Creates_New_Todo()
        {
            var newTodo  = new Todo(1, "New todo");
            var todoList = new List <Todo>();

            // Use When..Do to test void methods
            // When() allows us to call the method on the substitute and pass an argument
            // Do() allows us to pass a callback function that executes when the method is called
            todoRepo.When(t => t.Create(newTodo))
            .Do(t => todoList.Add(newTodo));

            todoRepo.GetAll().Returns(todoList);

            var result = underTest.Post(newTodo);

            Assert.Contains(newTodo, result);
        }
Exemplo n.º 13
0
        public void PostShouldReturnEntity()
        {
            var mockCurrentUser = new Mock <ICurrentUser>();

            mockCurrentUser.Setup(_ => _.UserId).Returns("USERID");

            var mockTodoRepository = new Mock <ITodoRepository>();

            mockTodoRepository.Setup(_ => _.Add(It.IsAny <Todo>())).
            Callback((Todo entity) => entity.Id = 1);

            var controller = new TodoController(mockCurrentUser.Object, mockTodoRepository.Object);

            var todo = controller.Post(new Todo());

            Assert.AreEqual(1, todo.Id);
            Assert.AreEqual("USERID", todo.UserId);
        }
Exemplo n.º 14
0
        public void UserFailTest()
        {
            // Arrange
            var mockServ   = new Mock <ITodoService>();
            var controller = new TodoController(mockServ.Object);

            // Act
            var actualGetResp    = controller.Get(null) as BadRequestResult;
            var actualPostResp   = controller.Post(null) as BadRequestResult;
            var actualPutResp    = controller.Put(0, null) as BadRequestResult;
            var actualDeleteResp = controller.Delete(0) as BadRequestResult;

            // Assert
            Assert.IsNotNull(actualGetResp);
            Assert.IsNotNull(actualPostResp);
            Assert.IsNotNull(actualPutResp);
            Assert.IsNotNull(actualDeleteResp);
        }
Exemplo n.º 15
0
        public async Task Can_400_When_Posting_An_Invalid_Todo(string task)
        {
            var fixture = new TodoDataFixture();
            var subject = new TodoController(fixture.Repository);
            var request = new TodoRequestApiModel {
                Task = task
            };
            var result = (await subject.Post(request)).Result as BadRequestObjectResult;

            Assert.NotNull(result);
            Assert.Equal(400, result.StatusCode);
            Assert.Equal(
                "failed_validation",
                ((ApiErrorModel)result.Value).Code
                );
            Assert.Equal(
                new[] { "Test Value" },
                fixture.Repository.Values.Values.Select(x => x.Task).ToArray()
                );
        }
Exemplo n.º 16
0
        public async Task Can_Post_A_Todo()
        {
            var fixture = new TodoDataFixture();
            var subject = new TodoController(fixture.Repository);
            var request = new TodoRequestApiModel {
                Task = "Created Value"
            };
            var result = (await subject.Post(request)).Result as OkObjectResult;

            Assert.NotNull(result);
            Assert.Equal(200, result.StatusCode);
            Assert.False(String.IsNullOrWhiteSpace(((TodoApiModel)result.Value).Id));
            Assert.Equal(
                request.Task,
                ((TodoApiModel)result.Value).Task
                );
            Assert.Equal(
                new[] { "Created Value", "Test Value" },
                fixture.Repository.Values.Values.Select(x => x.Task).OrderBy(x => x).ToArray()
                );
        }
        public void PostById_Positive_ReturnsCreated()
        {
            var  datarepo = new Mock <IDataRepo>();
            Note note     = new Note
            {
                NoteId = 4,
                Title  = "Testing Post"
            };

            datarepo.Setup(d => d.PostNote(note)).Returns(true);
            TodoController todoController = new TodoController(datarepo.Object);
            var            actionResult   = todoController.Post(note);

            var crObjectResult = actionResult as CreatedResult;

            Assert.NotNull(crObjectResult);

            var model = crObjectResult.Value as Note;

            Assert.Equal(note.NoteId, model.NoteId);
        }