Jakeuj's Notes master Help

例外測試

在進行測試時,我們需要確保應用程序在發生異常時能夠正確處理。在進行測試時,我們可以使用 Assert.ThrowsAsyncAssert.ThrowsAnyAsync 方法來確保應用程序在發生異常時能夠正確處理。

以下是一個示例,展示了如何在測試中使用 Assert.ThrowsAsync<AbpValidationException>Assert.ThrowsAnyAsync<Exception> 方法來確保應用程序在發生異常時能夠正確處理。

DemoSupplierAppServiceTests.cs

using Shouldly; using Volo.Abp.Json; using Volo.Abp.Modularity; using Volo.Abp.Validation; using Xunit; using Xunit.Abstractions; namespace Demo; /* This is just an example test class. * _testOutputHelper.WriteLine() is used to log the result of the test */ public abstract class DemoSupplierAppServiceTests<TStartupModule> : DemoAPIApplicationTestBase<TStartupModule> where TStartupModule : IAbpModule { private readonly DemoSupplierAppService _demoSupplierAppService; private readonly ITestOutputHelper _testOutputHelper; protected DemoSupplierAppServiceTests( ITestOutputHelper testOutputHelper) { _testOutputHelper = testOutputHelper; _demoSupplierAppService = GetRequiredService<DemoSupplierAppService>(); } [Fact] public async Task Should_Get_List_Of_Suppliers() { //Act var result = await _demoSupplierAppService.GetListAsync( new GetSupplierListDto() ); //Assert result.TotalCount.ShouldBeGreaterThan(0); result.Items.ShouldContain(b => b.Id == 1); result.Items.ShouldContain(b => b.Name == "Demo Company"); result.Items.ShouldContain(b => b.Code == 'M'); // Log _testOutputHelper.WriteLine( GetRequiredService<IJsonSerializer>().Serialize(result)); } [Fact] public async Task Should_Create_A_Valid_Supplier() { //Act var result = await _demoSupplierAppService.CreateAsync( new CreateUpdateSupplierDto { Name = "New test supplier 5", Code = 'T' } ); //Assert result.Id.ShouldBe(5); result.Name.ShouldBe("New test supplier 5"); result.Code.ShouldBe('T'); } [Fact] public async Task Should_Not_Create_A_Supplier_Without_Name() { var exception = await Assert .ThrowsAsync<AbpValidationException>(async () => { await _demoSupplierAppService.CreateAsync( new CreateUpdateSupplierDto { Name = "", Code = 'T' } ); }); exception.ValidationErrors .ShouldContain(err => err.MemberNames.Any(mem => mem == "Name")); } [Fact] public async Task Should_Update_A_Supplier_Name() { //Act var result = await _demoSupplierAppService.UpdateAsync(1, new CreateUpdateSupplierDto { Name = "New test supplier 5", Code = 'M' } ); //Assert result.Id.ShouldBe(1); result.Name.ShouldBe("New test supplier 5"); result.Code.ShouldBe('M'); } [Fact] public async Task Should_Update_A_Supplier_Code() { //Act var result = await _demoSupplierAppService.UpdateAsync(1, new CreateUpdateSupplierDto { Name = "Demo Supplier Company", Code = 'T' } ); //Assert result.Id.ShouldBe(1); result.Name.ShouldBe("Demo Supplier Company"); result.Code.ShouldBe('T'); } [Fact] public async Task Should_Not_Update_A_Supplier() { var exception = await Assert.ThrowsAsync<AbpValidationException>( async () => { await _demoSupplierAppService.UpdateAsync(1, new CreateUpdateSupplierDto()); }); exception.ValidationErrors .ShouldContain(err => err.MemberNames .Any(mem => mem == "Name")); } [Fact] public async Task Should_Not_Update_A_Supplier_Duplicate_Code() { var exception = await Assert.ThrowsAnyAsync<Exception>( async () => { //Act await _demoSupplierAppService.UpdateAsync(1, new CreateUpdateSupplierDto { Name = "Demo Supplier Company", Code = 'S' } ); }); //Assert exception.InnerException?.Message.ShouldContain( "UNIQUE constraint failed: DemoSuppliers.Code"); } [Fact] public async Task Should_Delete_A_Supplier() { //Act await _demoSupplierAppService.DeleteAsync(1); var result = await _demoSupplierAppService .GetListAsync(new GetSupplierListDto()); //Assert result.Items.ShouldNotContain(b => b.Id == 1); } }

EfCoreDemoSupplierAppServiceTests.cs

using Xunit; using Xunit.Abstractions; namespace Demo.EntityFrameworkCore.Applications.Demo; [Collection(DemoAPITestConsts.CollectionDefinitionName)] public class EfCoreDemoSupplierAppServiceTests : DemoSupplierAppServiceTests<DemoAPIEntityFrameworkCoreTestModule> { public EfCoreDemoSupplierAppServiceTests( ITestOutputHelper testOutputHelper ) : base(testOutputHelper) { } }
Last modified: 14 November 2024