public void DeleteTodo()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            _mockRepository.Setup(x => x.Delete(1)).Returns(true);

            controller.Delete(1);
        }
        public void GetAllTodos()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);
            var query = SetupDefaultODataQuery(controller);

            var allTodos = new List<TodoModel>
            {
                new TodoModel {completed = true, deadlineUtc = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1))}, // deadline was yesterday, but its already completed
                new TodoModel {completed = false, deadlineUtc = DateTime.UtcNow.Subtract(TimeSpan.FromDays(1))},// overdue
                new TodoModel {completed = false, deadlineUtc = DateTime.UtcNow.Add(TimeSpan.FromDays(1))}, // not due until tomorrow
                new TodoModel {completed = true, deadlineUtc = DateTime.UtcNow.Add(TimeSpan.FromDays(1))},  // not due until tomorrow
            };
            _mockRepository.Setup(x => x.GetAll()).Returns(allTodos);

            var response = controller.Get(query) as OkNegotiatedContentResult<List<TodoRepresentation>>;
            response.ShouldNotBeNull();
            response.Content.Count.ShouldEqual(4);
            AreEquivalent(allTodos, response.Content).ShouldBeTrue();
        }
        public void AddTodo()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);
            MockHelpers.SetupMockUrl(controller);

            TodoModel todo = new TodoModel
            {
                deadlineUtc = DateTime.UtcNow,
                completed = false,
                task = "Some task"
            };

            _mockRepository.Setup(x => x.Add(todo)).Returns(todo);

            var response = controller.Post(todo) as CreatedAtRouteNegotiatedContentResult<TodoRepresentation>;
            response.ShouldNotBeNull();
            response.RouteName.ShouldEqual("DefaultApi");
            response.RouteValues["id"].ShouldEqual(response.Content.id);
            AreEquivalent(todo, response.Content).ShouldBeTrue();

            _mockRepository.Verify(x => x.Add(todo), Times.Once());
        }
        public void ThrowNotFoundIfCantDeleteTodo()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            _mockRepository.Setup(x => x.Delete(1)).Returns(false);

            try
            {
                controller.Delete(1);
                Assert.Fail();
            }
            catch (HttpResponseException e)
            {
                e.Response.StatusCode.ShouldEqual(HttpStatusCode.NotFound);
            }
        }
        public void UpdateTodoOnPut()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            var todo = new TodoModel
            {
                id = 5,
                task = "My task"
            };

            var updatedTodo = new TodoModel
            {
                id = 5,
                task = "Updated task"
            };

            _mockRepository.Setup(x => x.Update(updatedTodo)).Returns(true);

            var response = controller.Put(todo.id, updatedTodo) as OkResult;
            response.ShouldNotBeNull();

            _mockRepository.Verify(x => x.Update(updatedTodo), Times.Once);
        }
        public void ReturnNotFoundIfCantFindTodo()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            var response = controller.Get(1) as NotFoundResult;
            response.ShouldNotBeNull();
        }
        public void ReturnBadRequestWhenPutIdDoesntMatchTodo()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            var response = controller.Put(5, new TodoModel {id = 4}) as BadRequestErrorMessageResult;
            response.ShouldNotBeNull();
        }
        public void ReturnBadRequestWhenPutCantUpdate()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            _mockRepository.Setup(x => x.Update(It.IsAny<TodoModel>())).Returns(false);

            var response = controller.Put(5, new TodoModel { id = 5 }) as BadRequestErrorMessageResult;
            response.ShouldNotBeNull();
            _mockRepository.Verify(x => x.Update(It.IsAny<TodoModel>()), Times.Once);
        }
 public void ReturnBadRequestIfEmptyBodyOnPut()
 {
     var controller = new TodosController(_mockRepository.Object, Mapper.Engine);
     var response = controller.Put(1, null) as BadRequestErrorMessageResult;
     response.ShouldNotBeNull();
 }
        public void GetTodo()
        {
            var controller = new TodosController(_mockRepository.Object, Mapper.Engine);

            TodoModel todo = new TodoModel
            {
                id = 3,
                deadlineUtc = DateTime.UtcNow,
                completed = false,
                task = "Some task"
            };

            _mockRepository.Setup(x => x.Get(todo.id)).Returns(todo);

            var response = controller.Get(todo.id) as OkNegotiatedContentResult<TodoRepresentation>;
            response.ShouldNotBeNull();
            AreEquivalent(todo, response.Content).ShouldBeTrue();
        }