diff --git a/AsbCloudWebApi.Tests/TestHelpter.cs b/AsbCloudWebApi.Tests/TestHelpter.cs index 430e6dc7..7df75b49 100644 --- a/AsbCloudWebApi.Tests/TestHelpter.cs +++ b/AsbCloudWebApi.Tests/TestHelpter.cs @@ -2,15 +2,18 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using Moq; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; namespace AsbCloudWebApi.Tests { internal static class TestHelpter { - // Попробовать когда-нибудь https://github.com/MichalJankowskii/Moq.EntityFrameworkCore - public static IMemoryCache MemoryCache = new MemoryCache(new MemoryCacheOptions()); public static AsbCloudDbContext MakeRealTestContext() @@ -30,22 +33,82 @@ namespace AsbCloudWebApi.Tests contextMock.Setup(o => o.Set()) .Returns(() => dbSetMock.Object); + var dbSetProperty = typeof(IAsbCloudDbContext) + .GetProperties() + .FirstOrDefault(p => p.PropertyType.IsPublic && p.PropertyType == typeof(DbSet)); + + if (dbSetProperty is not null) + { + // https://learn.microsoft.com/ru-ru/dotnet/api/system.linq.expressions.expression?view=net-7.0 + var objParameterExpr = Expression.Parameter(typeof(IAsbCloudDbContext), "instance"); + var propertyExpr = Expression.Property(objParameterExpr, dbSetProperty); + var expression = Expression.Lambda>>(propertyExpr, objParameterExpr); + contextMock.SetupGet(expression).Returns(dbSetMock.Object); + } + return contextMock; } public static Mock> MakeDbSetMock() where T : class + => MakeDbSetMock(new List()); + + class DummyAsyncQueriable : IQueryable, IAsyncEnumerable { - var dbSetData = new List(); - return MakeDbSetMock(dbSetData); + private readonly IQueryable queriable; + + public Type ElementType => queriable.ElementType; + + public Expression Expression => queriable.Expression; + + public IQueryProvider Provider => queriable.Provider; + + class QueriableAsyncEminaretor : IAsyncEnumerator + { + private readonly IEnumerator syncEnumerator; + + public QueriableAsyncEminaretor(IEnumerator syncEnumerator) + { + this.syncEnumerator = syncEnumerator; + } + + public T2 Current => syncEnumerator.Current; + + public ValueTask DisposeAsync() + { + syncEnumerator.Dispose(); + return ValueTask.CompletedTask; + } + + public ValueTask MoveNextAsync() + { + var result = syncEnumerator.MoveNext(); + return ValueTask.FromResult(result); + } + } + + public DummyAsyncQueriable(IEnumerable dbSetData) + { + queriable = dbSetData.ToList().AsQueryable(); + } + + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + var enumerator = this.AsEnumerable().GetEnumerator(); + return new QueriableAsyncEminaretor(enumerator); + } + + public IEnumerator GetEnumerator() + => queriable.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() + => queriable.GetEnumerator(); } public static Mock> MakeDbSetMock(IEnumerable dbSetData) where T : class { - var dbSetDataQueriable = dbSetData - .ToList() - .AsQueryable(); + var dbSetDataQueriable = new DummyAsyncQueriable(dbSetData); Mock> dbSetMock = new(); dbSetMock.As>().Setup(o => o.Provider).Returns(() => dbSetDataQueriable.Provider); @@ -53,6 +116,10 @@ namespace AsbCloudWebApi.Tests dbSetMock.As>().Setup(o => o.ElementType).Returns(() => dbSetDataQueriable.ElementType); dbSetMock.As>().Setup(o => o.GetEnumerator()).Returns(() => dbSetDataQueriable.GetEnumerator()); + dbSetMock.As>() + .Setup(o => o.GetAsyncEnumerator(It.IsAny())) + .Returns(() => dbSetDataQueriable.GetAsyncEnumerator()); + return dbSetMock; } }