diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs index 7f9a0540..32c44211 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/Orders/OrderAppServiceTests.cs @@ -1,5 +1,13 @@ -using Shouldly; +using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; +using EasyAbp.EShop.Orders.Orders.Dtos; +using EasyAbp.EShop.Products.Products; +using EasyAbp.EShop.Products.Products.Dtos; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; using Xunit; namespace EasyAbp.EShop.Orders.Orders @@ -8,19 +16,71 @@ namespace EasyAbp.EShop.Orders.Orders { private readonly IOrderAppService _orderAppService; + private readonly Guid _storeId = Guid.Parse("982C6439-AAC4-CD86-21E0-D89DFE066305"); + private readonly Guid _productId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA1"); + private readonly Guid _productSkuId = Guid.Parse("309A5529-A42A-9E0F-85FA-879B17B70EA2"); + public OrderAppServiceTests() { _orderAppService = GetRequiredService(); } + protected override void AfterAddApplication(IServiceCollection services) + { + var productAppService = Substitute.For(); + productAppService.GetAsync(_productId, _storeId).Returns(Task.FromResult(new ProductDto + { + CreationTime = DateTime.Now, + IsPublished = true, + Id = _productId, + ProductSkus = new List + { + new ProductSkuDto + { + Id = _productSkuId, + OrderMinQuantity = 0, + OrderMaxQuantity = 100, + AttributeOptionIds = new List() + } + }, + InventoryStrategy = InventoryStrategy.NoNeed + })); + + services.AddTransient(_ => productAppService); + } + [Fact] - public async Task Test1() + public async Task Should_Create_A_Order() { // Arrange + var createOrderDto = new CreateOrderDto + { + CustomerRemark = "customer remark", + StoreId = _storeId, + OrderLines = new List + { + new CreateOrderLineDto + { + ProductId = _productId, + ProductSkuId = _productSkuId, + Quantity = 10 + } + } + }; // Act + var response = await _orderAppService.CreateAsync(createOrderDto); // Assert + response.ShouldNotBeNull(); + response.CustomerRemark.ShouldBe("customer remark"); + + UsingDbContext(context => + { + context.Orders.Count().ShouldBe(1); + var order = context.Orders.First(); + order.CustomerRemark.ShouldBe("customer remark"); + }); } } -} +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs index e6fad2fe..b0c32162 100644 --- a/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs +++ b/modules/EasyAbp.EShop.Orders/test/EasyAbp.EShop.Orders.Application.Tests/OrdersApplicationTestBase.cs @@ -1,10 +1,22 @@ -namespace EasyAbp.EShop.Orders +using System; +using EasyAbp.EShop.Orders.EntityFrameworkCore; +using EasyAbp.EShop.Products.Products; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; + +namespace EasyAbp.EShop.Orders { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. */ public abstract class OrdersApplicationTestBase : OrdersTestBase { - + protected void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } } } \ No newline at end of file