diff --git a/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs b/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs index 809d4d3..58c1724 100644 --- a/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs +++ b/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs @@ -26,7 +26,7 @@ public static DbSet BuildMockDbSet(this IQueryable da mock.ConfigureDbSetCalls(data); if (mock is IAsyncEnumerable asyncEnumerable) { - A.CallTo(() => asyncEnumerable.GetAsyncEnumerator(A.Ignored)).Returns(enumerable.GetAsyncEnumerator()); + A.CallTo(() => asyncEnumerable.GetAsyncEnumerator(A.Ignored)).ReturnsLazily(() => enumerable.GetAsyncEnumerator()); } return mock; } diff --git a/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs b/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs index d81da1f..468b90b 100644 --- a/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs +++ b/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs @@ -23,7 +23,7 @@ public static Mock> BuildMockDbSet(this IQueryable(data); mock.ConfigureAsyncEnumerableCalls(enumerable); mock.As>().ConfigureQueryableCalls(enumerable, data); - mock.As>().Setup(x => x.GetAsyncEnumerator(It.IsAny())).Returns(enumerable.GetAsyncEnumerator()); + mock.As>().Setup(x => x.GetAsyncEnumerator(It.IsAny())).Returns(() => enumerable.GetAsyncEnumerator()); mock.Setup(m => m.AsQueryable()).Returns(enumerable); mock.ConfigureDbSetCalls(data); diff --git a/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs b/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs index 46a56ba..e74d2ee 100644 --- a/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs +++ b/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using FakeItEasy; +using Microsoft.EntityFrameworkCore; using MockQueryable.FakeItEasy; using NUnit.Framework; @@ -246,7 +247,25 @@ public async Task DbSetCreatedFromCollectionGetAllUserEntitiesAsync() Assert.AreEqual(users.Count, result.Count); } - [TestCase] + [TestCase] + public async Task DbSetToListAsyncAsync_ShouldReturnAllEntities_WhenSourceIsChanged() + { + // arrange + var users = new List(); + + 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() { //arrange diff --git a/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs b/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs index fc38f63..dbf8e9b 100644 --- a/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs +++ b/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs @@ -360,6 +360,27 @@ public async Task DbSetGetAllUserEntitiesAsync() // assert Assert.AreEqual(users.Count, result.Count); } + + + [TestCase] + public async Task DbSetToListAsyncAsync_ShouldReturnAllEntities_WhenSourceIsChanged() + { + // arrange + var users = new List(); + + 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); + } + + [TestCase] public async Task DbSetCreatedFromCollectionGetAllUserEntitiesAsync() diff --git a/src/MockQueryable/MockQueryable.Sample/MyServiceNSubstituteTests.cs b/src/MockQueryable/MockQueryable.Sample/MyServiceNSubstituteTests.cs index 35caa2a..1f9e08c 100644 --- a/src/MockQueryable/MockQueryable.Sample/MyServiceNSubstituteTests.cs +++ b/src/MockQueryable/MockQueryable.Sample/MyServiceNSubstituteTests.cs @@ -265,6 +265,26 @@ public async Task DbSetGetAllUserEntitiesAsync() // assert Assert.AreEqual(users.Count, result.Count); } + + + [TestCase] + public async Task DbSetGetAllUserEntitiesAsync_ShouldReturnAllEntities_WhenSourceIsChanged() + { + // arrange + var users = new List(); + + 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 DbSetCreatedFromCollectionGetAllUserEntitiesAsync()