Beispiel #1
0
        public void GetTodoItemsList_ReturnAllTodoListItems()
        {
            var ctx      = new TestTodoAppContext();
            var todoList = GenSampleTodoItemsList();

            foreach (var item in todoList)
            {
                ctx.Items.Add(item);
            }
            var controller = new TodoItemsController(ctx)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            var response = controller.GetTodoItems();

            Assert.IsNotNull(response);
            Assert.IsTrue(response.IsSuccessStatusCode);
            string result;

            Assert.IsTrue(response.TryGetContentValue <string>(out result));
            List <TodoItem> todoItems = JsonConvert.DeserializeObject <List <TodoItem> >(result);

            Assert.AreEqual(7, todoItems.Count);
        }
Beispiel #2
0
        public void GetTodoItem_Return404()
        {
            var ctx        = new TestTodoAppContext();
            var sampleItem = ValidTodoItem();

            ctx.Items.Add(sampleItem);
            var controller = new TodoItemsController(ctx)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };
            var response = controller.GetTodoItem(2); //as OkNegotiatedContentResult<TodoItem>;

            Assert.IsNotNull(response);
            Assert.IsFalse(response.IsSuccessStatusCode);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.NotFound);
        }
Beispiel #3
0
        public void PatchTodoItem_ReturnsUpdatedItem()
        {
            var ctx        = new TestTodoAppContext();
            var controller = new TodoItemsController(ctx)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/api/TodoItems")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
            controller.RequestContext.RouteData = new HttpRouteData(
                route: new HttpRoute(),
                values: new HttpRouteValueDictionary {
                { "controller", "TodoItems" }
            });

            var sampleItem = ValidTodoItem();

            ctx.Items.Add(sampleItem);
            var patchItem = new TodoItemPatch()
            {
                Title       = "Pick the car form the dealership",
                Description = "Car is in the workshop and will be out from service",
                DueDate     = DateTime.Now.AddDays(3)
            };
            var response = controller.PatchTodoItem(sampleItem.Id, patchItem);

            Assert.IsNotNull(response);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.OK);
            string result;

            Assert.IsTrue(response.TryGetContentValue <string>(out result));
            TodoItem resultItem = JsonConvert.DeserializeObject <TodoItem>(result);

            Assert.AreEqual(sampleItem.Id, resultItem.Id);
            Assert.AreEqual(patchItem.Description, resultItem.Description);
            Assert.AreEqual(patchItem.Title, resultItem.Title);
        }
Beispiel #4
0
        public void GetTodoItem_ReturnItemWithSameId()
        {
            var ctx        = new TestTodoAppContext();
            var sampleItem = ValidTodoItem();

            ctx.Items.Add(sampleItem);
            var controller = new TodoItemsController(ctx)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };
            var response = controller.GetTodoItem(1); //as OkNegotiatedContentResult<TodoItem>;

            Assert.IsNotNull(response);
            Assert.IsTrue(response.IsSuccessStatusCode);
            string result;

            Assert.IsTrue(response.TryGetContentValue <string>(out result));
            TodoItem item = JsonConvert.DeserializeObject <TodoItem>(result);

            Assert.AreEqual(1, item.Id);
        }
Beispiel #5
0
        public void PatchTodoItem_Returns404()
        {
            var ctx        = new TestTodoAppContext();
            var controller = new TodoItemsController(ctx)
            {
                Request = new HttpRequestMessage
                {
                    RequestUri = new Uri("http://localhost/api/TodoItems")
                },
                Configuration = new HttpConfiguration()
            };

            controller.Configuration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
                );
            controller.RequestContext.RouteData = new HttpRouteData(
                route: new HttpRoute(),
                values: new HttpRouteValueDictionary {
                { "controller", "TodoItems" }
            });

            var sampleItem = ValidTodoItem();
            //sampleItem not added to the context
            var patchItem = new TodoItemPatch()
            {
                Title       = "Pick the car form the dealership",
                Description = "Car is in the workshop and will be out from service",
                DueDate     = DateTime.Now.AddDays(3)
            };
            var response = controller.PatchTodoItem(sampleItem.Id, patchItem);

            Assert.IsNotNull(response);
            Assert.AreEqual(response.StatusCode, HttpStatusCode.NotFound);
        }