diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs index 034e71e2..d553c885 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/Products/ProductAppServiceTests.cs @@ -1,5 +1,10 @@ +using System.Collections.Generic; +using System.Linq; using Shouldly; using System.Threading.Tasks; +using EasyAbp.EShop.Products.Options; +using EasyAbp.EShop.Products.Products.Dtos; +using Microsoft.Extensions.Options; using Xunit; namespace EasyAbp.EShop.Products.Products @@ -7,20 +12,66 @@ namespace EasyAbp.EShop.Products.Products public class ProductAppServiceTests : ProductsApplicationTestBase { private readonly IProductAppService _productAppService; + private readonly EShopProductsOptions _eShopProductsOptions; public ProductAppServiceTests() { _productAppService = GetRequiredService(); + _eShopProductsOptions = GetRequiredService>().Value; } [Fact] - public async Task Test1() + public async Task Should_Create_A_Product_With_SKU() { // Arrange + _eShopProductsOptions.Groups.Configure("Default Group Name", x => + { + x.DisplayName = "Default Group Name"; + x.Description = "Default Description"; + }); + + var requestDto = new CreateUpdateProductDto + { + ProductGroupName = "Default Group Name", + ProductDetailId = ProductsTestData.ProductDetails1Id, + StoreId = ProductsTestData.Store1Id, + UniqueName = "Unique Pencil", + DisplayName = "Pencil", + InventoryStrategy = InventoryStrategy.NoNeed, + DisplayOrder = 0, + IsPublished = true, + ProductAttributes = new List + { + new CreateUpdateProductAttributeDto + { + DisplayName = "Default Attribute 1", + Description = "Default Description 1", + DisplayOrder = 1, + ProductAttributeOptions = new List + { + new CreateUpdateProductAttributeOptionDto + { + DisplayName = "Option?" + } + } + } + } + }; // Act + var response = await _productAppService.CreateAsync(requestDto); // Assert + response.ShouldNotBeNull(); + response.DisplayName.ShouldBe("Pencil"); + response.UniqueName.ShouldBe("Unique Pencil"); + + UsingDbContext(db => + { + var product = db.Products.FirstOrDefault(x => x.Id == response.Id); + product.ShouldNotBeNull(); + product.DisplayName.ShouldBe("Pencil"); + }); } } -} +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs index 2c773c37..d61aa13d 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.Application.Tests/ProductsApplicationTestBase.cs @@ -1,10 +1,19 @@ -namespace EasyAbp.EShop.Products +using System; +using EasyAbp.EShop.Products.EntityFrameworkCore; + +namespace EasyAbp.EShop.Products { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. */ public abstract class ProductsApplicationTestBase : ProductsTestBase { - + protected void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs index 7bfbba49..330bbe21 100644 --- a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/EShopProductsTestBaseModule.cs @@ -35,6 +35,10 @@ namespace EasyAbp.EShop.Products await scope.ServiceProvider .GetRequiredService() .SeedAsync(); + + await scope.ServiceProvider + .GetRequiredService() + .BuildAsync(); } }); } diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs new file mode 100644 index 00000000..6dfaba4b --- /dev/null +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestData.cs @@ -0,0 +1,11 @@ +using System; + +namespace EasyAbp.EShop.Products +{ + public class ProductsTestData + { + public static Guid Store1Id { get; } = Guid.NewGuid(); + + public static Guid ProductDetails1Id { get; } = Guid.NewGuid(); + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs new file mode 100644 index 00000000..f315c55b --- /dev/null +++ b/modules/EasyAbp.EShop.Products/test/EasyAbp.EShop.Products.TestBase/ProductsTestDataBuilder.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using EasyAbp.EShop.Products.ProductDetails; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; + +namespace EasyAbp.EShop.Products +{ + public class ProductsTestDataBuilder : ITransientDependency + { + private readonly IProductDetailRepository _productDetailRepository; + + public ProductsTestDataBuilder(IProductDetailRepository productDetailRepository) + { + _productDetailRepository = productDetailRepository; + } + + public void Build() + { + AsyncHelper.RunSync(BuildAsync); + } + + public async Task BuildAsync() + { + await _productDetailRepository.InsertAsync(new ProductDetail(ProductsTestData.ProductDetails1Id, "Product Details")); + } + } +} \ No newline at end of file