Skip to content

Commit

Permalink
Merge branch 'master' into DbSetShortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
romantitov authored Mar 7, 2024
2 parents d25ba79 + 7a660d6 commit 103e0ef
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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 @@ -23,7 +23,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
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 @@ -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<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()
{
//arrange
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 @@ -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<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);
}



[TestCase]
public async Task DbSetCreatedFromCollectionGetAllUserEntitiesAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 DbSetCreatedFromCollectionGetAllUserEntitiesAsync()
Expand Down

0 comments on commit 103e0ef

Please sign in to comment.