From 9d6ad6a3d3b05f361222c6ce68d81b873dce38c0 Mon Sep 17 00:00:00 2001 From: GameBelial <243387971@qq.com> Date: Tue, 8 Sep 2020 16:32:16 +0800 Subject: [PATCH] Complete the basic unit test of the stores module. --- .../Stores/StoreAppServiceTests.cs | 29 ++++++++++++++++++- .../StoresApplicationTestBase.cs | 13 +++++++-- .../StoresTestData.cs | 7 +++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs index 4a2b3947..3b9f8799 100644 --- a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/Stores/StoreAppServiceTests.cs @@ -1,5 +1,7 @@ +using System.Linq; using Shouldly; using System.Threading.Tasks; +using EasyAbp.EShop.Stores.Stores.Dtos; using Xunit; namespace EasyAbp.EShop.Stores.Stores @@ -14,13 +16,38 @@ namespace EasyAbp.EShop.Stores.Stores } [Fact] - public async Task Test1() + public async Task Should_Create_A_Store() { // Arrange + var request = new CreateUpdateStoreDto + { + Name = "New Store 2" + }; // Act + var response = await _storeAppService.CreateAsync(request); // Assert + response.ShouldNotBeNull(); + response.Name.ShouldBe("New Store 2"); + + UsingDbContext(db => + { + var store = db.Stores.FirstOrDefault(x=>x.Id == response.Id); + store.ShouldNotBeNull(); + store.Name.ShouldBe("New Store 2"); + }); + } + + [Fact] + public async Task Should_Return_Default_Store() + { + // Arrange & Act + var response = await _storeAppService.GetDefaultAsync(); + + // Assert + response.ShouldNotBeNull(); + response.Name.ShouldBe("My Store"); } } } diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs index 29b3751d..14ead32c 100644 --- a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.Application.Tests/StoresApplicationTestBase.cs @@ -1,10 +1,19 @@ -namespace EasyAbp.EShop.Stores +using System; +using EasyAbp.EShop.Stores.EntityFrameworkCore; + +namespace EasyAbp.EShop.Stores { /* Inherit from this class for your application layer tests. * See SampleAppService_Tests for example. */ public abstract class StoresApplicationTestBase : StoresTestBase { - + protected void UsingDbContext(Action action) + { + using (var dbContext = GetRequiredService()) + { + action.Invoke(dbContext); + } + } } } \ No newline at end of file diff --git a/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs new file mode 100644 index 00000000..60278156 --- /dev/null +++ b/modules/EasyAbp.EShop.Stores/test/EasyAbp.EShop.Stores.TestBase/StoresTestData.cs @@ -0,0 +1,7 @@ +namespace EasyAbp.EShop.Stores +{ + public static class StoresTestData + { + + } +} \ No newline at end of file