零度框架 1.0 版本正式发布,共 184 项改进。
This commit is contained in:
1 parent
c14a872b77
commit
baf06cd88f
182 files changed
+2537
-1613
No files matched your search
@@ -0,0 +1,62 @@
|
||||
// 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<IProductService> mock = new();
|
||||
|
||||
mock.Setup(m => m.FindAsync(It.Is<int>(id => id == 1), It.IsAny<CancellationToken>())).ReturnsAsync((Product?)null);
|
||||
|
||||
// Act
|
||||
IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap<Product, ProductDetailsResponse>()));
|
||||
MockProductsController mockProductsController = new(mock.Object, mapper);
|
||||
ActionResult<ProductDetailsResponse> result = await mockProductsController.GetProduct(1);
|
||||
|
||||
//Assert
|
||||
Assert.IsType<NotFoundResult>(result.Result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllReturnsPorductListItems()
|
||||
{
|
||||
// Arrange
|
||||
Mock<IProductService> mock = new();
|
||||
|
||||
mock.Setup(m => m.GetAllAsync(It.IsAny<CancellationToken>())).ReturnsAsync([new Product { Id = 1, Name = "Product 1", Price = 10 }, new Product { Id = 2, Name = "Product 2", Price = 20 }]);
|
||||
|
||||
// Act
|
||||
IMapper mapper = new Mapper(new MapperConfiguration(cfg => cfg.CreateMap<Product, ProductListItem>()));
|
||||
MockProductsController mockProductsController = new(mock.Object, mapper);
|
||||
ActionResult<IEnumerable<ProductListItem>> 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user