Exemplo n.º 1
0
        public async void PostArticleInvalidIdTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            var article    = new ArticleDTO(_articleDTOs.First());
            int originalId = article.Id;

            article.Lead = "Change";

            //Id does not match article Id
            var result = await controller.PostArticle(article.Id + 1, article);

            Assert.IsType <BadRequestResult>(result);


            //Id matches but does not exist
            article.Id = 40;

            var result2 = await controller.PostArticle(article.Id, article);

            Assert.IsType <NotFoundResult>(result2);


            //GET TEST (Article unchanged)
            var result3 = await controller.GetArticle(originalId);

            var objectResult3 = Assert.IsType <OkObjectResult>(result3);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult3.Value);

            Assert.Equal(_articleDTOs.First(), model);
        }
Exemplo n.º 2
0
        public void ControlerShouldNotPostNewArticle()
        {
            var article = new Article
            {
                Title       = "Web API Unit Testing",
                URL         = "https://chsakell.com/web-api-unit-testing",
                Author      = "Chris Sakellarios",
                DateCreated = DateTime.Now,
                Contents    = null
            };

            var _articlesController = new ArticlesController(articleService)
            {
                Configuration = new HttpConfiguration(),
                Request       = new HttpRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://localhost/api/articles")
                }
            };

            _articlesController.Configuration.MapHttpAttributeRoutes();
            _articlesController.Configuration.EnsureInitialized();
            _articlesController.RequestContext.RouteData = new HttpRouteData(
                new HttpRoute(), new HttpRouteValueDictionary {
                { "Controller", "Articles" }
            });
            _articlesController.ModelState.AddModelError("Contents", "Contents is required field");

            var result = _articlesController.PostArticle(article) as InvalidModelStateResult;

            Assert.That(result.ModelState.Count, Is.EqualTo(1));
            Assert.That(result.ModelState.IsValid, Is.EqualTo(false));
        }
Exemplo n.º 3
0
        public void ControlerShouldPostNewArticle()
        {
            var article = new Article
            {
                Title       = "Web API Unit Testing",
                URL         = "https://chsakell.com/web-api-unit-testing",
                Author      = "Chris Sakellarios",
                DateCreated = DateTime.Now,
                Contents    = "Unit testing Web API.."
            };

            var _articlesController = new ArticlesController(articleService)
            {
                Configuration = new HttpConfiguration(),
                Request       = new HttpRequestMessage
                {
                    Method     = HttpMethod.Post,
                    RequestUri = new Uri("http://localhost/api/articles")
                }
            };

            _articlesController.Configuration.MapHttpAttributeRoutes();
            _articlesController.Configuration.EnsureInitialized();
            _articlesController.RequestContext.RouteData = new HttpRouteData(
                new HttpRoute(), new HttpRouteValueDictionary {
                { "_articlesController", "Articles" }
            });
            var result = _articlesController.PostArticle(article) as CreatedAtRouteNegotiatedContentResult <Article>;

            Assert.That(result.RouteName, Is.EqualTo("DefaultApi"));
            Assert.That(result.Content.ID, Is.EqualTo(result.RouteValues["id"]));
            Assert.That(result.Content.ID, Is.EqualTo(randomArticles.Max(a => a.ID)));
        }
Exemplo n.º 4
0
        public async void PostArticleEmptyFieldTest()
        {
            var controller = new ArticlesController(_context, _userManager);

            //POST TEST 1 (Lead empty)
            var article = new ArticleDTO(_articleDTOs.First());

            article.Lead = "";

            var result = await controller.PostArticle(article.Id, article);

            var objectResult = Assert.IsType <BadRequestResult>(result);

            //POST TEST 2 (Name empty)
            var article2 = new ArticleDTO(_articleDTOs.First());

            article.Name = "";

            var result2 = await controller.PostArticle(article.Id, article);

            var objectResult2 = Assert.IsType <BadRequestResult>(result2);

            //POST TEST 3 (Content empty)
            var article3 = new ArticleDTO(_articleDTOs.First());

            article.Content = "";

            var result3 = await controller.PostArticle(article.Id, article);

            var objectResult3 = Assert.IsType <BadRequestResult>(result3);


            //GET TEST (Article unchanged)
            var result4 = await controller.GetArticle(article.Id);

            var objectResult4 = Assert.IsType <OkObjectResult>(result4);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult4.Value);

            Assert.Equal(_articleDTOs.First(), model);
        }
        public void TestCreateArticle()
        {
            var controller = new ArticlesController();

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            var article = unsavedArticle();

            var response = controller.PostArticle(article);

            Assert.AreEqual(response.ToString(), "System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult`1[Blog.Models.ArticleDto]");
        }
Exemplo n.º 6
0
        public async void PostArticleTest()
        {
            var controller = new ArticlesController(_context, _userManager);
            var article    = _articleDTOs.First();

            article.Name = "Changed test name";
            article.Lead = "Changed up the lead as well";
            article.Images.RemoveAt(0);

            var result = await controller.PostArticle(article.Id, article);

            // Assert
            var objectResult = Assert.IsType <NoContentResult>(result);

            var result2 = await controller.GetArticle(article.Id);

            var objectResult2 = Assert.IsType <OkObjectResult>(result2);
            var model         = Assert.IsAssignableFrom <ArticleDTO>(objectResult2.Value);

            Assert.Equal(article, model);
        }