using System; using System.Linq; using System.Threading.Tasks; using Shouldly; using Volo.Abp.Application.Dtos; using Volo.Abp.Modularity; using Volo.Abp.Validation; using Xunit;
[Fact] publicasync Task Should_Create_A_Valid_Book() { //Act var result = await _bookAppService.CreateAsync( new CreateUpdateBookDto { Name = "New test book 42", Price = 10, PublishDate = DateTime.Now, Type = BookType.ScienceFiction } );
//Assert result.Id.ShouldNotBe(Guid.Empty); result.Name.ShouldBe("New test book 42"); }
新增测试方法,创建一个错误book实体失败的场景:
由于 Name 是空值,ABP 抛出一个 AbpValidationException 异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[Fact] publicasync Task Should_Not_Create_A_Book_Without_Name() { var exception = await Assert.ThrowsAsync<AbpValidationException>(async () => { await _bookAppService.CreateAsync( new CreateUpdateBookDto { Name = "", Price = 10, PublishDate = DateTime.Now, Type = BookType.ScienceFiction } ); });
exception.ValidationErrors .ShouldContain(err => err.MemberNames.Any(mem => mem == "Name")); }
using System; using System.Linq; using System.Threading.Tasks; using Shouldly; using Volo.Abp.Application.Dtos; using Volo.Abp.Validation; using Xunit;
[Fact] publicasync Task Should_Create_A_Valid_Book() { //Act var result = await _bookAppService.CreateAsync( new CreateUpdateBookDto { Name = "New test book 42", Price = 10, PublishDate = DateTime.Now, Type = BookType.ScienceFiction } );
//Assert result.Id.ShouldNotBe(Guid.Empty); result.Name.ShouldBe("New test book 42"); }
[Fact] publicasync Task Should_Not_Create_A_Book_Without_Name() { var exception = await Assert.ThrowsAsync<AbpValidationException>(async () => { await _bookAppService.CreateAsync( new CreateUpdateBookDto { Name = "", Price = 10, PublishDate = DateTime.Now, Type = BookType.ScienceFiction } ); });