From b9b58505540aa3bb98b086b9767b1c7738418e8f Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 8 Sep 2020 19:10:22 +0800 Subject: [PATCH] Complete the basic unit test of the payments module. --- .../Payments/PaymentAppServiceTests.cs | 43 ++++++++++++++++--- .../Refunds/RefundAppServiceTests.cs | 27 +++++++++--- .../PaymentsTestData.cs | 15 +++++++ .../PaymentsTestDataBuilder.cs | 28 ++++++++++++ 4 files changed, 103 insertions(+), 10 deletions(-) create mode 100644 modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs create mode 100644 modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs index d8c7dd39..8272e796 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Payments/PaymentAppServiceTests.cs @@ -1,5 +1,11 @@ -using Shouldly; +using System; +using System.Collections.Generic; using System.Threading.Tasks; +using EasyAbp.EShop.Orders.Orders; +using EasyAbp.EShop.Orders.Orders.Dtos; +using EasyAbp.EShop.Payments.Payments.Dtos; +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; using Xunit; namespace EasyAbp.EShop.Payments.Payments @@ -8,19 +14,46 @@ namespace EasyAbp.EShop.Payments.Payments { private readonly IPaymentAppService _paymentAppService; + protected override void AfterAddApplication(IServiceCollection services) + { + MockOrderService(services); + } + + private void MockOrderService(IServiceCollection services) + { + var orderService = Substitute.For(); + orderService.GetAsync(PaymentsTestData.Order1).Returns(Task.FromResult(new OrderDto + { + Id = PaymentsTestData.Order1, + Currency = "$", + TotalDiscount = 10, + TotalPrice = 100, + StoreId = PaymentsTestData.Store1 + })); + + services.AddTransient(_ => orderService); + } + public PaymentAppServiceTests() { _paymentAppService = GetRequiredService(); } [Fact] - public async Task Test1() + public async Task Should_Success_Create_Payment() { // Arrange + var request = new CreatePaymentDto + { + OrderIds = new List + { + PaymentsTestData.Order1 + }, + PaymentMethod = "Alipay" + }; - // Act - - // Assert + // Act & Assert + await _paymentAppService.CreateAsync(request); } } } diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs index 626e117c..23a1ad86 100644 --- a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.Application.Tests/Refunds/RefundAppServiceTests.cs @@ -1,5 +1,6 @@ -using Shouldly; +using System.Collections.Generic; using System.Threading.Tasks; +using EasyAbp.EShop.Payments.Refunds.Dtos; using Xunit; namespace EasyAbp.EShop.Payments.Refunds @@ -13,14 +14,30 @@ namespace EasyAbp.EShop.Payments.Refunds _refundAppService = GetRequiredService(); } + // TODO: There may be BUG. [Fact] - public async Task Test1() + public async Task Should_Refund_Success() { // Arrange + var request = new CreateEShopRefundInput + { + DisplayReason = "Reason", + CustomerRemark = "Customer Remark", + PaymentId = PaymentsTestData.Payment1, + StaffRemark = "StaffRemark", + RefundItems = new List + { + new CreateEShopRefundItemInput + { + CustomerRemark = "CustomerRemark", + OrderId = PaymentsTestData.Order1, + StaffRemark = "StaffRemark" + } + } + }; - // Act - - // Assert + // Act & Assert + await _refundAppService.CreateAsync(request); } } } diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs new file mode 100644 index 00000000..da43ca82 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestData.cs @@ -0,0 +1,15 @@ +using System; + +namespace EasyAbp.EShop.Payments +{ + public static class PaymentsTestData + { + public static Guid Order1 { get; } = Guid.NewGuid(); + + public static Guid Order2 { get; } = Guid.NewGuid(); + + public static Guid Store1 { get; } = Guid.NewGuid(); + + public static Guid Payment1 { get; } = Guid.NewGuid(); + } +} \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs new file mode 100644 index 00000000..a29cb9a2 --- /dev/null +++ b/modules/EasyAbp.EShop.Payments/test/EasyAbp.EShop.Payments.TestBase/PaymentsTestDataBuilder.cs @@ -0,0 +1,28 @@ +using System.Threading.Tasks; +using EasyAbp.EShop.Payments.Payments; +using Volo.Abp.DependencyInjection; +using Volo.Abp.Threading; + +namespace EasyAbp.EShop.Payments +{ + public class PaymentsTestDataBuilder : ITransientDependency + { + private readonly IPaymentRepository _paymentRepository; + + public PaymentsTestDataBuilder(IPaymentRepository paymentRepository) + { + _paymentRepository = paymentRepository; + } + + public void Build() + { + AsyncHelper.RunSync(BuildAsync); + } + + public async Task BuildAsync() + { + // TODO: How to create payment? + // await _paymentRepository.InsertAsync(new Payment()); + } + } +} \ No newline at end of file