Esempio n. 1
0
        public async Task PostCategory_ReturnsFromErrorController400_WhenException()
        {
            //ARRANGE
            //1. User input
            var newCategory2 = new CategoryEditCreateDTO
            {
                CategoryName = "een nieuwe categorie" //moet een fout veroozaken
                                                      // Id = 10000
            };

            //2. Mocking van controller en alle controller methodes ()
            // .Returns(Task.FromResult(mapper.Map<CategoryEditCreateDTO, Category>(newCategory2)));
            mockCategoryRepo.Setup(repo => repo.Create(It.IsAny <Category>())).Throws(new Exception());

            var APIcontroller = new CategoriesController(mockCategoryRepo.Object, mockRepo.Object, mapper, null, memoryCache, null);  //alle args en ‘.Object’ niet vergeten

            //3. controleer of mockRepo opgeroepen werd;
            mockCategoryRepo.Verify();

            //ACT : controller oproepen
            var actionResult = await APIcontroller.PostCategory(newCategory2);

            var redirectToActionResult = (RedirectToActionResult)actionResult.Result;

            //ASSERT:
            Assert.IsNotNull(redirectToActionResult);
            Assert.AreEqual(redirectToActionResult.ActionName, "HandleErrorCode");
            Assert.AreEqual(redirectToActionResult.ControllerName, "Error");
            Assert.IsTrue(redirectToActionResult.RouteValues["ErrorMessage"].ToString().Contains("is mislukt"));
        }
Esempio n. 2
0
        public async Task PostCategory_Returns_Category_WhenModelIsValid()
        {
            //ARRANGE
            //1. User input
            var newCategory = new CategoryEditCreateDTO
            {
                CategoryName = "een nieuwe categorie"
                               // Id = 10000
            };

            //2. Mocking van controller en alle controller methodes ()
            mockCategoryRepo.Setup(repo => repo.Create(It.IsAny <Category>()))
            .Returns(Task.FromResult(mapper.Map <CategoryEditCreateDTO, Category>(newCategory)));

            var APIcontroller = new CategoriesController(mockCategoryRepo.Object, mockRepo.Object, mapper, null, memoryCache, null);  //alle args en ‘.Object’ niet vergeten

            //3. controleer of mockRepo opgeroepen werd;
            mockCategoryRepo.Verify();

            //ACT : controller oproepen
            var actionResult = await APIcontroller.PostCategory(newCategory);

            var createdResult = (CreatedAtActionResult)actionResult.Result;

            //ASSERT
            Assert.IsNotNull(createdResult);                                                                    //null
            Assert.IsInstanceOfType(createdResult, typeof(CreatedAtActionResult));                              //type
            Assert.IsInstanceOfType(createdResult.Value, typeof(CategoryEditCreateDTO));
            Assert.AreEqual(((CategoryEditCreateDTO)createdResult.Value).CategoryName, "een nieuwe categorie"); //data
            Assert.AreEqual("GetCategoryById", createdResult.ActionName);
            Assert.AreEqual(201, createdResult.StatusCode);                                                     //statuscode
        }
        public async Task <ActionResult <CategoryEditCreateDTO> > PostCategory([FromBody][Bind("CategoryName")] CategoryEditCreateDTO categoryDTO)
        {
            if (categoryDTO == null)
            {
                return(BadRequest(new { Message = "Geen categorie input" }));
            }
            ;

            var category = mapper.Map <Category>(categoryDTO);

            if (category == null)
            {
                return(BadRequest(new { Message = "Onvoldoende data bij de categorie" }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                //_context.Category.Add(category);
                //await _context.SaveChangesAsync();
                await genericRepo.Create(category);

                return(CreatedAtAction("GetCategoryById", new { id = category.Id }, mapper.Map <CategoryEditCreateDTO>(category)));
            }
            catch (Exception exc)
            {
                //Serilog
                Log.Logger.Warning(LogEventIds.InsertData.ToString() + $" - CategorieController: Exceptie bij toevoegen van {category.CategoryName}: {exc.Message}");

                //Customised gebruikers error
                return(RedirectToAction("HandleErrorCode", "Error", new
                {
                    statusCode = 400,
                    errorMessage = $"Het bewaren van categorie '{category.CategoryName}' is mislukt."
                }));
            }
        }