public async Task RunSample() { List <string> myList = new List <string> { "Akos1", "Bkos2", "Akos3" }; var inMemoryQueryable = new InMemoryAsyncQueryable <string>(myList); var result = await inMemoryQueryable.Where(m => m.StartsWith("A")).ToListAsync(); }
/// <summary> /// Setup data to <see cref="DbSet{T}"/>. /// </summary> /// <typeparam name="TEntity">The entity type.</typeparam> /// <param name="dbSet">The <see cref="DbSet{T}"/>.</param> /// <param name="data">The seed data.</param> /// <param name="find">The find action.</param> /// <returns>The updated <see cref="DbSet{T}"/>.</returns> public static DbSet <TEntity> SetupData <TEntity>(this DbSet <TEntity> dbSet, ICollection <TEntity> data = null, Func <object[], TEntity> find = null) where TEntity : class { data = data ?? new List <TEntity>(); find = find ?? (o => null); var query = new InMemoryAsyncQueryable <TEntity>(data.AsQueryable()); A.CallTo(() => ((IQueryable <TEntity>)dbSet).Provider).ReturnsLazily(info => query.Provider); A.CallTo(() => ((IQueryable <TEntity>)dbSet).Expression).ReturnsLazily(info => query.Expression); A.CallTo(() => ((IQueryable <TEntity>)dbSet).ElementType).ReturnsLazily(info => query.ElementType); A.CallTo(() => ((IQueryable <TEntity>)dbSet).GetEnumerator()).ReturnsLazily(info => query.GetEnumerator()); #if !NET40 A.CallTo(() => ((IDbAsyncEnumerable <TEntity>)dbSet).GetAsyncEnumerator()).ReturnsLazily(info => query.GetAsyncEnumerator()); #endif A.CallTo(() => dbSet.Include(A <string> ._)).Returns(dbSet); A.CallTo(() => dbSet.Find(A <object[]> ._)).ReturnsLazily <TEntity, object[]>(objs => find(objs)); #if !NET40 A.CallTo(() => dbSet.FindAsync(A <object[]> ._)).ReturnsLazily <Task <TEntity>, object[]>(objs => Task.Run(() => find(objs))); A.CallTo(() => dbSet.FindAsync(A <CancellationToken> ._, A <object[]> ._)).ReturnsLazily <Task <TEntity>, CancellationToken, object[]>((token, objs) => Task.Run(() => find(objs), token)); #endif A.CallTo(() => dbSet.Remove(A <TEntity> ._)).ReturnsLazily <TEntity, TEntity>(entity => { data.Remove(entity); return(entity); }); A.CallTo(() => dbSet.RemoveRange(A <IEnumerable <TEntity> > ._)).ReturnsLazily <IEnumerable <TEntity>, IEnumerable <TEntity> >(entities => { foreach (var entity in entities) { data.Remove(entity); } return(entities); }); A.CallTo(() => dbSet.Add(A <TEntity> ._)).ReturnsLazily <TEntity, TEntity>(entity => { data.Add(entity); return(entity); }); A.CallTo(() => dbSet.AddRange(A <IEnumerable <TEntity> > ._)).ReturnsLazily <IEnumerable <TEntity>, IEnumerable <TEntity> >(entities => { foreach (var entity in entities) { data.Add(entity); } return(entities); }); return(dbSet); }
/// <summary> /// Setup data to <see cref="Mock{T}"/>. /// </summary> /// <typeparam name="TEntity">The entity type.</typeparam> /// <param name="mock">The <see cref="Mock{T}"/>.</param> /// <param name="data">The seed data.</param> /// <param name="find">The find action.</param> /// <returns>The updated <see cref="Mock{T}"/>.</returns> public static Mock <DbSet <TEntity> > SetupData <TEntity>(this Mock <DbSet <TEntity> > mock, ICollection <TEntity> data = null, Func <object[], TEntity> find = null) where TEntity : class { data = data ?? new List <TEntity>(); find = find ?? (o => null); var query = new InMemoryAsyncQueryable <TEntity>(data.AsQueryable()); mock.As <IQueryable <TEntity> >().Setup(m => m.Provider).Returns(query.Provider); mock.As <IQueryable <TEntity> >().Setup(m => m.Expression).Returns(query.Expression); mock.As <IQueryable <TEntity> >().Setup(m => m.ElementType).Returns(query.ElementType); mock.As <IQueryable <TEntity> >().Setup(m => m.GetEnumerator()).Returns(query.GetEnumerator); #if !NET40 mock.As <IDbAsyncEnumerable <TEntity> >().Setup(m => m.GetAsyncEnumerator()).Returns(query.GetAsyncEnumerator); #endif mock.Setup(m => m.Include(It.IsAny <string>())).Returns(mock.Object); mock.Setup(m => m.Find(It.IsAny <object[]>())).Returns <object[]>(find); #if !NET40 mock.Setup(m => m.FindAsync(It.IsAny <object[]>())).Returns <object[]>(objs => Task.Run(() => find(objs))); mock.Setup(m => m.FindAsync(It.IsAny <CancellationToken>(), It.IsAny <object[]>())).Returns <CancellationToken, object[]>((tocken, objs) => Task.Run(() => find(objs), tocken)); #endif mock.Setup(m => m.Remove(It.IsAny <TEntity>())) .Callback <TEntity>(entity => { data.Remove(entity); mock.SetupData(data, find); }) .Returns <TEntity>(entity => entity); mock.Setup(m => m.RemoveRange(It.IsAny <IEnumerable <TEntity> >())) .Callback <IEnumerable <TEntity> >(entities => { foreach (var entity in entities) { data.Remove(entity); } mock.SetupData(data, find); }) .Returns <IEnumerable <TEntity> >(entities => entities); mock.Setup(m => m.Add(It.IsAny <TEntity>())) .Callback <TEntity>(entity => { data.Add(entity); mock.SetupData(data, find); }) .Returns <TEntity>(entity => entity); mock.Setup(m => m.AddRange(It.IsAny <IEnumerable <TEntity> >())) .Callback <IEnumerable <TEntity> >(entities => { foreach (var entity in entities) { data.Add(entity); } ; mock.SetupData(data, find); }) .Returns <IEnumerable <TEntity> >(entities => entities); return(mock); }