Comments on: Testing Repository Pattern Using Entity Framework https://code-maze.com/testing-repository-pattern-entity-framework/ Learn. Code. Succeed. Mon, 19 Sep 2022 19:55:58 +0000 hourly 1 https://wordpress.org/?v=6.7.5 By: Marinko Spasojević https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6619 Mon, 19 Sep 2022 19:55:58 +0000 https://drafts.code-maze.com/?p=70535#comment-6619 In reply to Qais.

Well usually, the update action returns 204. But there are some cases in which it returns an updated entity. I think whatever you choose you won’t make a mistake. But if your requirement is for the PUT action to return only the status code if it is successful, then just leave it with 204.

]]>
By: Qais https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6618 Mon, 19 Sep 2022 14:52:48 +0000 https://drafts.code-maze.com/?p=70535#comment-6618 In reply to MarinkoSpasojevic.

Thank you for your valuable time. I tried to implement put using following code.

// Arrange
            ILogger<ProductCategoryController> logger = GetLogger();
            var _unitOfWork = MockIUnitOfWork.GetMock();
            var productcategoryController = new ProductCategoryController(_unitOfWork.Object, logger);
            var productCategory = new ProductCategory()
            {
                ProductCategoryId=1,
                CategoryName = "Skin Care"
            };
            // Act
            var result = await productcategoryController.UpdateProductCategory(1, productCategory) as ObjectResult;
            // Assert
            Assert.NotNull(result);
            Assert.Equal(StatusCodes.Status204NoContent, result!.StatusCode);

But it is returning null. The update and delete methods just return a status code while the create method returns the added entity. Should I modify these two methods as well like the create method?
Lets say if it works after updating. The question is, should I really update it to make it work as we usually return NoContent(204) on update or delete.

 [HttpPut("{id}")]
        public async Task<IActionResult> UpdateProductCategory(int id, [FromBody] ProductCategory productCategory)
        {
            try
            {
                if (id != productCategory.ProductCategoryId)
                    return NotFound();


                if (ModelState.IsValid)
                {
                    if (!_unitOfWork.ProductCategoryRepo.ProductCategoryExists(productCategory.ProductCategoryId))
                        return NotFound();


                    _unitOfWork.ProductCategoryRepo.Update(productCategory);
                    await _unitOfWork.SaveAsync();
                    return NoContent();
                }
                return BadRequest();
            }
            catch (Exception ex)
            {
                string error = ErrorBuilder.BuildErrorMessage(ex);
                _logger.LogError(MyLogEvents.UpdateItem, error);
                return StatusCode(StatusCodes.Status500InternalServerError);
            }
        }
]]>
By: MarinkoSpasojevic https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6617 Mon, 19 Sep 2022 07:35:09 +0000 https://drafts.code-maze.com/?p=70535#comment-6617 In reply to Qais.

🙂 Well yeah. Maybe someone can find my answer inappropriate like I don’t want to spend time to reply the reader, but it is not like that. The best way to learn something is to try it by yourself, find the issue and fix it. Knowledge gained that way stays much longer in our heads 🙂

]]>
By: Qais https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6616 Mon, 19 Sep 2022 07:17:19 +0000 https://drafts.code-maze.com/?p=70535#comment-6616 In reply to Marinko Spasojević.

Thanks, Now I know the reason 🙂

]]>
By: Marinko Spasojević https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6599 Tue, 13 Sep 2022 17:05:18 +0000 https://drafts.code-maze.com/?p=70535#comment-6599 In reply to Qais.

To be honest, the best way to answer that is to use this example without mocking those two and inspect the result.

]]>
By: Qais https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6597 Tue, 13 Sep 2022 15:14:46 +0000 https://drafts.code-maze.com/?p=70535#comment-6597 I am wondering why are we mocking Account and Owner repository when we are not using it in our tests.

]]>
By: Marinko Spasojević https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6021 Fri, 08 Jul 2022 06:47:57 +0000 https://drafts.code-maze.com/?p=70535#comment-6021 In reply to Pedro Dias.

Hello Pedro. To be honest, I didn’t try it, but maybe this can help: https://code-maze.com/csharp-mock-asynchronous-methods-using-moq/. If only being async is an issue, I think this should be a solution. Again, I really didn’t try it, just wanted to mention it.

]]>
By: Pedro Dias https://code-maze.com/testing-repository-pattern-entity-framework/#comment-6017 Fri, 08 Jul 2022 04:04:17 +0000 https://drafts.code-maze.com/?p=70535#comment-6017 You can indeed mock dbsets, however, if you want to support the async linq operations, such as FirstOrDefaultAsync, then things start to become more complicated.

]]>