romantitov / MockQueryable

Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc
MIT License
791 stars 77 forks source link

Unable to mock result of IAsyncEnumerable from DbSet using NSubstitute #49

Closed krilllind closed 3 years ago

krilllind commented 3 years ago

Mocking an IAsyncEnumerable result from a DbSet using NSubstitute does not work and instead yield an empty result set. A fix for this exact same issue was adressed in #25 but only for the Moq library. Same override should be applied when using NSubstitute.

The following code snipped should return all users from a DbSet

List<UserEntity> users = new List<UserEntity> { new UserEntity() };
DbSet<UserEntity> mockDbSet = users.AsQueryable().BuildMockDbSet();
dbContext.Users.Returns(mockDbSet);

List<UserEntity> result = await userRepository.GetAllUsersAsync().ToListAsync(cancellationToken);

Assert.AreEqual(users.Count, result.Count);

Instead, an empty collection is returned.

romantitov commented 3 years ago

Thank you @krilllind for the issue and especially for the PR. I'll review it and make changes available in a new release.

romantitov commented 3 years ago

The issue fixed in release 5.0.1