using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Util.Tests.Fakes; using Util.Tests.Infrastructure; using Util.Tests.Repositories; using Util.Tests.UnitOfWorks; using Xunit; namespace Util.Data.EntityFrameworkCore.Tests; /// /// 贴子仓储测试 /// 说明: /// 1. 测试多对多 /// public class PostRepositoryTest : TestBase { /// /// 仓储 /// private readonly IPostRepository _repository; /// /// 测试初始化 /// public PostRepositoryTest(IPostRepository repository, ITestUnitOfWork unitOfWork ) : base(unitOfWork){ _repository = repository; } /// /// 测试添加实体 /// [Fact] public async Task TestAddAsync() { //添加实体 var post = PostFakeService.GetPost(); post.Init(); var tag1 = TagFakeService.GetTag(); tag1.Init(); post.Tags.Add( tag1 ); var tag2 = TagFakeService.GetTag(); tag2.Init(); post.Tags.Add( tag2 ); await _repository.AddAsync( post ); await UnitOfWork.CommitAsync(); UnitOfWork.ClearCache(); //验证 var result = await _repository.SingleAsync( t => t.Id == post.Id, queryable => queryable.Include( t => t.Tags ) ); Assert.NotNull( result ); Assert.Equal( 2,result.Tags.Count ); } }