// Copyright (c) HelloShop Corporation. All rights reserved. // See the license file in the project root for more information. using AutoMapper; using HelloShop.ProductService.Controllers; using HelloShop.ProductService.Entities.Products; using HelloShop.ProductService.Models.Products; using HelloShop.ProductService.Services; using Microsoft.AspNetCore.Mvc; using Moq; namespace HelloShop.ProductService.UnitTests { public class MockProductsControllerTest { [Fact] public async Task GetProductReturnsNotFoundIfNotExists() { // Arrange Mock mock = new(); mock.Setup(m => m.FindAsync(It.Is(id => id == 1), It.IsAny())).ReturnsAsync((Product?)null); // Act IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap())); MockProductsController mockProductsController = new(mock.Object, mapper); ActionResult result = await mockProductsController.GetProduct(1); //Assert Assert.IsType(result.Result); } [Fact] public async Task GetAllReturnsPorductListItems() { // Arrange Mock mock = new(); var mockProducts = new List { new() { Id = 1, Name = "Product 1", Description = "Product 1", ImageUrl = "1.jpg", Price = 10 }, new() { Id = 2, Name = "Product 2",Description="Product 2", ImageUrl="2.jpg", Price = 20 } }; mock.Setup(m => m.GetAllAsync(It.IsAny())).ReturnsAsync(mockProducts); // Act IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap())); MockProductsController mockProductsController = new(mock.Object, mapper); ActionResult> result = await mockProductsController.GetProducts(); //Assert Assert.NotNull(result.Value); Assert.Collection(result.Value, productListItem1 => { Assert.Equal(1, productListItem1.Id); Assert.Equal("Product 1", productListItem1.Name); Assert.Equal(10, productListItem1.Price); }, productListItem2 => { Assert.Equal(2, productListItem2.Id); Assert.Equal("Product 2", productListItem2.Name); Assert.Equal(20, productListItem2.Price); }); } } }