Skip to content

Commit

Permalink
Merge pull request #74 from fischero19/master
Browse files Browse the repository at this point in the history
Fix ToListAsync() for mocked DbSet by using lazy-creation of AsyncEnumerator
  • Loading branch information
romantitov authored Mar 7, 2024
2 parents 8fddf5f + e17fae5 commit 7a660d6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static DbSet<TEntity> BuildMockDbSet<TEntity>(this IQueryable<TEntity> da
mock.ConfigureDbSetCalls(data);
if (mock is IAsyncEnumerable<TEntity> asyncEnumerable)
{
A.CallTo(() => asyncEnumerable.GetAsyncEnumerator(A<CancellationToken>.Ignored)).Returns(enumerable.GetAsyncEnumerator());
A.CallTo(() => asyncEnumerable.GetAsyncEnumerator(A<CancellationToken>.Ignored)).ReturnsLazily(() => enumerable.GetAsyncEnumerator());
}
return mock;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static Mock<DbSet<TEntity>> BuildMockDbSet<TEntity>(this IQueryable<TEnti
var enumerable = new TestAsyncEnumerableEfCore<TEntity>(data);
mock.ConfigureAsyncEnumerableCalls(enumerable);
mock.As<IQueryable<TEntity>>().ConfigureQueryableCalls(enumerable, data);
mock.As<IAsyncEnumerable<TEntity>>().Setup(x => x.GetAsyncEnumerator(It.IsAny<CancellationToken>())).Returns(enumerable.GetAsyncEnumerator());
mock.As<IAsyncEnumerable<TEntity>>().Setup(x => x.GetAsyncEnumerator(It.IsAny<CancellationToken>())).Returns(() => enumerable.GetAsyncEnumerator());
mock.Setup(m => m.AsQueryable()).Returns(enumerable);

mock.ConfigureDbSetCalls(data);
Expand Down
19 changes: 19 additions & 0 deletions src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading;
using System.Threading.Tasks;
using FakeItEasy;
using Microsoft.EntityFrameworkCore;
using MockQueryable.FakeItEasy;
using NUnit.Framework;

Expand Down Expand Up @@ -165,6 +166,24 @@ public async Task DbSetGetAllUserEntitiesAsync()
Assert.AreEqual(users.Count, result.Count);
}

[TestCase]
public async Task DbSetToListAsyncAsync_ShouldReturnAllEntities_WhenSourceIsChanged()
{
// arrange
var users = new List<UserEntity>();

var mockDbSet = users.AsQueryable().BuildMockDbSet();

// act
var result1 = await mockDbSet.ToListAsync();
users.AddRange(CreateUserList());
var result2 = await mockDbSet.ToListAsync();

// assert
Assert.AreEqual(0, result1.Count);
Assert.AreEqual(users.Count, result2.Count);
}

[TestCase]
public async Task DbSetGetAllUserEntity()
{
Expand Down
21 changes: 21 additions & 0 deletions src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,27 @@ public async Task DbSetGetAllUserEntitiesAsync()
// assert
Assert.AreEqual(users.Count, result.Count);
}


[TestCase]
public async Task DbSetToListAsyncAsync_ShouldReturnAllEntities_WhenSourceIsChanged()
{
// arrange
var users = new List<UserEntity>();

var mockDbSet = users.AsQueryable().BuildMockDbSet();

// act
var result1 = await mockDbSet.Object.ToListAsync();
users.AddRange(CreateUserList());
var result2 = await mockDbSet.Object.ToListAsync();

// assert
Assert.AreEqual(0, result1.Count);
Assert.AreEqual(users.Count, result2.Count);
}



private static List<UserEntity> CreateUserList() => new List<UserEntity>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,26 @@ public async Task DbSetGetAllUserEntitiesAsync()
// assert
Assert.AreEqual(users.Count, result.Count);
}


[TestCase]
public async Task DbSetGetAllUserEntitiesAsync_ShouldReturnAllEntities_WhenSourceIsChanged()
{
// arrange
var users = new List<UserEntity>();

var mockDbSet = users.AsQueryable().BuildMockDbSet();
var userRepository = new TestDbSetRepository(mockDbSet);

// act
var result1 = await userRepository.GetAllAsync().ToListAsync();
users.AddRange(CreateUserList());
var result2 = await userRepository.GetAllAsync().ToListAsync();

// assert
Assert.AreEqual(0, result1.Count);
Assert.AreEqual(users.Count, result2.Count);
}

[TestCase]
public async Task DbSetGetOneUserTntityAsync()
Expand Down

0 comments on commit 7a660d6

Please sign in to comment.