diff --git a/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs b/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs
index 009ae10..53fb457 100644
--- a/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs
+++ b/src/MockQueryable/MockQueryable.FakeItEasy/FakeItEasyExtensions.cs
@@ -10,95 +10,95 @@
namespace MockQueryable.FakeItEasy
{
- public static class FakeItEasyExtensions
- {
-
- ///
- /// This method allows you to create a mock DbSet for testing purposes.
- /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
- /// with custom expression handling, such as for testing LINQ queries or database operations.
- /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
- /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
- /// and LINQ query capabilities.
- ///
- ///
- /// The type of the entity that the DbSet will represent.
- ///
- public static DbSet BuildMockDbSet(this ICollection data) where TEntity : class
+ public static class FakeItEasyExtensions
{
- return BuildMockDbSet(data);
- }
- ///
- /// This method allows you to create a mock DbSet for testing purposes.
- /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
- /// with custom expression handling, such as for testing LINQ queries or database operations.
- /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
- /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
- /// and LINQ query capabilities.
- ///
- ///
- /// The type of the entity that the DbSet will represent.
- ///
- ///
- /// The type of the expression visitor that will be used to process LINQ expressions.
- /// Can be used to mock EF Core specific expression handling, such as for ILike expressions.
- ///
- public static DbSet BuildMockDbSet(this ICollection data)
- where TEntity : class
- where TExpressionVisitor : ExpressionVisitor, new()
- {
- var mock = A.Fake>(d => d.Implements>().Implements>());
- var enumerable = new TestAsyncEnumerableEfCore(data, entity => data.Remove(entity));
- mock.ConfigureQueryableCalls(enumerable, data.AsQueryable());
- mock.ConfigureAsyncEnumerableCalls(enumerable);
- mock.ConfigureDbSetCalls(data.AsQueryable());
+ ///
+ /// This method allows you to create a mock DbSet for testing purposes.
+ /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
+ /// with custom expression handling, such as for testing LINQ queries or database operations.
+ /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
+ /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
+ /// and LINQ query capabilities.
+ ///
+ ///
+ /// The type of the entity that the DbSet will represent.
+ ///
+ public static DbSet BuildMockDbSet(this ICollection data) where TEntity : class
+ {
+ return BuildMockDbSet(data);
+ }
- if (mock is IAsyncEnumerable asyncEnumerable)
- {
- A.CallTo(() => asyncEnumerable.GetAsyncEnumerator(A.Ignored)).ReturnsLazily(() => enumerable.GetAsyncEnumerator());
- }
+ ///
+ /// This method allows you to create a mock DbSet for testing purposes.
+ /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
+ /// with custom expression handling, such as for testing LINQ queries or database operations.
+ /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
+ /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
+ /// and LINQ query capabilities.
+ ///
+ ///
+ /// The type of the entity that the DbSet will represent.
+ ///
+ ///
+ /// The type of the expression visitor that will be used to process LINQ expressions.
+ /// Can be used to mock EF Core specific expression handling, such as for ILike expressions.
+ ///
+ public static DbSet BuildMockDbSet(this ICollection data)
+ where TEntity : class
+ where TExpressionVisitor : ExpressionVisitor, new()
+ {
+ var mock = A.Fake>(d => d.Implements>().Implements>());
+ var enumerable = new TestAsyncEnumerableEfCore(data, entity => data.Remove(entity));
+ mock.ConfigureQueryableCalls(enumerable, data.AsQueryable());
+ mock.ConfigureAsyncEnumerableCalls(enumerable);
+ mock.ConfigureDbSetCalls(data.AsQueryable());
- return mock;
- }
+ if (mock is IAsyncEnumerable asyncEnumerable)
+ {
+ A.CallTo(() => asyncEnumerable.GetAsyncEnumerator(A.Ignored)).ReturnsLazily(() => enumerable.GetAsyncEnumerator());
+ }
- private static void ConfigureQueryableCalls(
- this IQueryable mock,
- IQueryProvider queryProvider,
- IQueryable data) where TEntity : class
- {
- A.CallTo(() => mock.Provider).Returns(queryProvider);
- A.CallTo(() => mock.Expression).Returns(data?.Expression);
- A.CallTo(() => mock.ElementType).Returns(data?.ElementType);
- A.CallTo(() => mock.GetEnumerator()).ReturnsLazily(() => data?.GetEnumerator());
- }
+ return mock;
+ }
- private static void ConfigureAsyncEnumerableCalls(
- this DbSet mock,
- IAsyncEnumerable enumerable) where TEntity : class
- {
- A.CallTo(() => mock.GetAsyncEnumerator(A.Ignored))
- .Returns(enumerable.GetAsyncEnumerator());
- }
+ private static void ConfigureQueryableCalls(
+ this IQueryable mock,
+ IQueryProvider queryProvider,
+ IQueryable data) where TEntity : class
+ {
+ A.CallTo(() => mock.Provider).Returns(queryProvider);
+ A.CallTo(() => mock.Expression).Returns(data?.Expression);
+ A.CallTo(() => mock.ElementType).Returns(data?.ElementType);
+ A.CallTo(() => mock.GetEnumerator()).ReturnsLazily(() => data?.GetEnumerator());
+ }
- private static void ConfigureDbSetCalls(this DbSet mock, IQueryable data)
- where TEntity : class
- {
- A.CallTo(() => mock.AsQueryable()).Returns(data.AsQueryable());
- A.CallTo(() => mock.AsAsyncEnumerable()).ReturnsLazily(args => CreateAsyncMock(data));
- }
+ private static void ConfigureAsyncEnumerableCalls(
+ this DbSet mock,
+ IAsyncEnumerable enumerable) where TEntity : class
+ {
+ A.CallTo(() => mock.GetAsyncEnumerator(A.Ignored))
+ .Returns(enumerable.GetAsyncEnumerator());
+ }
- private static async IAsyncEnumerable CreateAsyncMock(
- this IEnumerable data)
- where TEntity : class
- {
+ private static void ConfigureDbSetCalls(this DbSet mock, IQueryable data)
+ where TEntity : class
+ {
+ A.CallTo(() => mock.AsQueryable()).Returns(mock);
+ A.CallTo(() => mock.AsAsyncEnumerable()).ReturnsLazily(args => CreateAsyncMock(data));
+ }
+
+ private static async IAsyncEnumerable CreateAsyncMock(
+ this IEnumerable data)
+ where TEntity : class
+ {
- foreach (var entity in data)
- {
- yield return entity;
- }
+ foreach (var entity in data)
+ {
+ yield return entity;
+ }
- await Task.CompletedTask;
+ await Task.CompletedTask;
+ }
}
- }
}
\ No newline at end of file
diff --git a/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs b/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs
index 23e655f..d76beec 100644
--- a/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs
+++ b/src/MockQueryable/MockQueryable.Moq/MoqExtensions.cs
@@ -10,92 +10,90 @@
namespace MockQueryable.Moq
{
- public static class MoqExtensions
- {
-
- ///
- /// This method allows you to create a mock DbSet for testing purposes.
- /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
- /// with custom expression handling, such as for testing LINQ queries or database operations.
- /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
- /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
- /// and LINQ query capabilities.
- ///
- ///
- /// The type of the entity that the DbSet will represent.
- ///
- public static Mock> BuildMockDbSet(this ICollection data) where TEntity : class
+ public static class MoqExtensions
{
- return BuildMockDbSet(data);
- }
- ///
- /// This method allows you to create a mock DbSet for testing purposes.
- /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
- /// with custom expression handling, such as for testing LINQ queries or database operations.
- /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
- /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
- /// and LINQ query capabilities.
- ///
- ///
- /// The type of the entity that the DbSet will represent.
- ///
- ///
- /// The type of the expression visitor that will be used to process LINQ expressions.
- /// Can be used to mock EF Core specific expression handling, such as for ILike expressions.
- ///
- public static Mock> BuildMockDbSet(this ICollection data)
- where TEntity : class
- where TExpressionVisitor : ExpressionVisitor, new()
- {
- var mock = new Mock>();
- var enumerable = new TestAsyncEnumerableEfCore(data, entity => data.Remove(entity));
- mock.ConfigureAsyncEnumerableCalls(enumerable);
- mock.As>().ConfigureQueryableCalls(enumerable, data.AsQueryable());
- mock.As>().Setup(x => x.GetAsyncEnumerator(It.IsAny())).Returns(() => enumerable.GetAsyncEnumerator());
- mock.Setup(m => m.AsQueryable()).Returns(enumerable);
+ ///
+ /// This method allows you to create a mock DbSet for testing purposes.
+ /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
+ /// with custom expression handling, such as for testing LINQ queries or database operations.
+ /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
+ /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
+ /// and LINQ query capabilities.
+ ///
+ ///
+ /// The type of the entity that the DbSet will represent.
+ ///
+ public static Mock> BuildMockDbSet(this ICollection data) where TEntity : class
+ {
+ return BuildMockDbSet(data);
+ }
- mock.ConfigureDbSetCalls(data.AsQueryable());
- return mock;
- }
+ ///
+ /// This method allows you to create a mock DbSet for testing purposes.
+ /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
+ /// with custom expression handling, such as for testing LINQ queries or database operations.
+ /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
+ /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
+ /// and LINQ query capabilities.
+ ///
+ ///
+ /// The type of the entity that the DbSet will represent.
+ ///
+ ///
+ /// The type of the expression visitor that will be used to process LINQ expressions.
+ /// Can be used to mock EF Core specific expression handling, such as for ILike expressions.
+ ///
+ public static Mock> BuildMockDbSet(this ICollection data)
+ where TEntity : class
+ where TExpressionVisitor : ExpressionVisitor, new()
+ {
+ var mock = new Mock>();
+ var enumerable = new TestAsyncEnumerableEfCore(data, entity => data.Remove(entity));
+ mock.ConfigureAsyncEnumerableCalls(enumerable);
+ mock.As>().ConfigureQueryableCalls(enumerable, data.AsQueryable());
+ mock.As>().Setup(x => x.GetAsyncEnumerator(It.IsAny())).Returns(() => enumerable.GetAsyncEnumerator());
+ mock.Setup(m => m.AsQueryable()).Returns(enumerable);
- private static void ConfigureDbSetCalls(this Mock> mock, IQueryable data)
- where TEntity : class
- {
- mock.Setup(m => m.AsQueryable()).Returns(mock.Object);
- mock.Setup(m => m.AsAsyncEnumerable()).Returns(CreateAsyncMock(data));
- }
+ mock.ConfigureDbSetCalls(data.AsQueryable());
+ return mock;
+ }
- private static void ConfigureQueryableCalls(
- this Mock> mock,
- IQueryProvider queryProvider,
- IQueryable data) where TEntity : class
- {
- mock.Setup(m => m.Provider).Returns(queryProvider);
- mock.Setup(m => m.Expression).Returns(data?.Expression);
- mock.Setup(m => m.ElementType).Returns(data?.ElementType);
- mock.Setup(m => m.GetEnumerator()).Returns(() => data?.GetEnumerator());
+ private static void ConfigureDbSetCalls(this Mock> mock, IQueryable data)
+ where TEntity : class
+ {
+ mock.Setup(m => m.AsQueryable()).Returns(mock.Object);
+ mock.Setup(m => m.AsAsyncEnumerable()).Returns(CreateAsyncMock(data));
+ }
- }
+ private static void ConfigureQueryableCalls(
+ this Mock> mock,
+ IQueryProvider queryProvider,
+ IQueryable data) where TEntity : class
+ {
+ mock.Setup(m => m.Provider).Returns(queryProvider);
+ mock.Setup(m => m.Expression).Returns(data?.Expression);
+ mock.Setup(m => m.ElementType).Returns(data?.ElementType);
+ mock.Setup(m => m.GetEnumerator()).Returns(() => data?.GetEnumerator());
+ }
- private static void ConfigureAsyncEnumerableCalls(
- this Mock> mock,
- IAsyncEnumerable enumerable) where TEntity : class
- {
- mock.Setup(d => d.GetAsyncEnumerator(It.IsAny()))
- .Returns(() => enumerable.GetAsyncEnumerator());
-
- }
+ private static void ConfigureAsyncEnumerableCalls(
+ this Mock> mock,
+ IAsyncEnumerable enumerable) where TEntity : class
+ {
+ mock.Setup(d => d.GetAsyncEnumerator(It.IsAny()))
+ .Returns(() => enumerable.GetAsyncEnumerator());
+ }
- private static async IAsyncEnumerable CreateAsyncMock(IEnumerable data)
- where TEntity : class
- {
- foreach (var entity in data)
- {
- yield return entity;
- }
+ private static async IAsyncEnumerable CreateAsyncMock(IEnumerable data)
+ where TEntity : class
+ {
+ foreach (var entity in data)
+ {
+ yield return entity;
+ }
- await Task.CompletedTask;
+ await Task.CompletedTask;
+ }
}
- }
}
\ No newline at end of file
diff --git a/src/MockQueryable/MockQueryable.NSubstitute/NSubstituteExtensions.cs b/src/MockQueryable/MockQueryable.NSubstitute/NSubstituteExtensions.cs
index 5056907..9a5dd71 100644
--- a/src/MockQueryable/MockQueryable.NSubstitute/NSubstituteExtensions.cs
+++ b/src/MockQueryable/MockQueryable.NSubstitute/NSubstituteExtensions.cs
@@ -10,105 +10,105 @@
namespace MockQueryable.NSubstitute
{
- public static class NSubstituteExtensions
- {
-
- ///
- /// This method allows you to create a mock DbSet for testing purposes.
- /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
- /// with custom expression handling, such as for testing LINQ queries or database operations.
- /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
- /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
- /// and LINQ query capabilities.
- ///
- ///
- /// The type of the entity that the DbSet will represent.
- ///
- public static DbSet BuildMockDbSet(this ICollection data) where TEntity : class
+ public static class NSubstituteExtensions
{
- return BuildMockDbSet(data);
- }
- ///
- /// This method allows you to create a mock DbSet for testing purposes.
- /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
- /// with custom expression handling, such as for testing LINQ queries or database operations.
- /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
- /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
- /// and LINQ query capabilities.
- ///
- ///
- /// The type of the entity that the DbSet will represent.
- ///
- ///
- /// The type of the expression visitor that will be used to process LINQ expressions.
- /// Can be used to mock EF Core specific expression handling, such as for ILike expressions.
- ///
- public static DbSet BuildMockDbSet(this ICollection data)
- where TEntity : class
- where TExpressionVisitor : ExpressionVisitor, new()
- {
- var mock = Substitute.For, IQueryable, IAsyncEnumerable>();
- var enumerable = new TestAsyncEnumerableEfCore(data, entity => data.Remove(entity));
+ ///
+ /// This method allows you to create a mock DbSet for testing purposes.
+ /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
+ /// with custom expression handling, such as for testing LINQ queries or database operations.
+ /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
+ /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
+ /// and LINQ query capabilities.
+ ///
+ ///
+ /// The type of the entity that the DbSet will represent.
+ ///
+ public static DbSet BuildMockDbSet(this ICollection data) where TEntity : class
+ {
+ return BuildMockDbSet(data);
+ }
- ConfigureAllCalls(mock, enumerable, data);
+ ///
+ /// This method allows you to create a mock DbSet for testing purposes.
+ /// It is particularly useful when you want to simulate the behavior of Entity Framework Core's DbSet
+ /// with custom expression handling, such as for testing LINQ queries or database operations.
+ /// The method takes an IQueryable of the entity type and returns a mocked DbSet that implements
+ /// both IAsyncEnumerable and IQueryable interfaces, allowing for asynchronous enumeration
+ /// and LINQ query capabilities.
+ ///
+ ///
+ /// The type of the entity that the DbSet will represent.
+ ///
+ ///
+ /// The type of the expression visitor that will be used to process LINQ expressions.
+ /// Can be used to mock EF Core specific expression handling, such as for ILike expressions.
+ ///
+ public static DbSet BuildMockDbSet(this ICollection data)
+ where TEntity : class
+ where TExpressionVisitor : ExpressionVisitor, new()
+ {
+ var mock = Substitute.For, IQueryable, IAsyncEnumerable>();
+ var enumerable = new TestAsyncEnumerableEfCore(data, entity => data.Remove(entity));
- return mock;
- }
+ ConfigureAllCalls(mock, enumerable, data);
-
+ return mock;
+ }
- private static void ConfigureAllCalls(
- this DbSet mock,
- TestAsyncEnumerableEfCore enumerable,
- ICollection data)
- where TEntity : class
- where TExpressionVisitor : ExpressionVisitor, new()
- {
- mock.ConfigureAsyncEnumerableCalls(enumerable);
- mock.ConfigureQueryableCalls(enumerable, data.AsQueryable());
- mock.ConfigureDbSetCalls(data.AsQueryable());
- if (mock is IAsyncEnumerable asyncEnumerable)
- {
- asyncEnumerable.GetAsyncEnumerator(Arg.Any()).Returns(args => enumerable.GetAsyncEnumerator());
- }
- }
- private static void ConfigureQueryableCalls(
- this IQueryable mock,
- IQueryProvider queryProvider,
- IQueryable data) where TEntity : class
- {
- mock.Provider.Returns(queryProvider);
- mock.Expression.Returns(data?.Expression);
- mock.ElementType.Returns(data?.ElementType);
- mock.GetEnumerator().Returns(info => data?.GetEnumerator());
- }
+ private static void ConfigureAllCalls(
+ this DbSet mock,
+ TestAsyncEnumerableEfCore enumerable,
+ ICollection data)
+ where TEntity : class
+ where TExpressionVisitor : ExpressionVisitor, new()
+ {
+ mock.ConfigureAsyncEnumerableCalls(enumerable);
+ mock.ConfigureQueryableCalls(enumerable, data.AsQueryable());
+ mock.ConfigureDbSetCalls(data.AsQueryable());
- private static void ConfigureAsyncEnumerableCalls(
- this DbSet mock,
- IAsyncEnumerable enumerable) where TEntity : class
- {
- mock.GetAsyncEnumerator(Arg.Any()).Returns(args => enumerable.GetAsyncEnumerator());
- }
+ if (mock is IAsyncEnumerable asyncEnumerable)
+ {
+ asyncEnumerable.GetAsyncEnumerator(Arg.Any()).Returns(args => enumerable.GetAsyncEnumerator());
+ }
+ }
- private static void ConfigureDbSetCalls(this DbSet mock, IQueryable data)
- where TEntity : class
- {
- mock.AsQueryable().Returns(data);
- mock.AsAsyncEnumerable().Returns(args => CreateAsyncMock(data));
- }
+ private static void ConfigureQueryableCalls(
+ this IQueryable mock,
+ IQueryProvider queryProvider,
+ IQueryable data) where TEntity : class
+ {
+ mock.Provider.Returns(queryProvider);
+ mock.Expression.Returns(data?.Expression);
+ mock.ElementType.Returns(data?.ElementType);
+ mock.GetEnumerator().Returns(info => data?.GetEnumerator());
+ }
- private static async IAsyncEnumerable CreateAsyncMock(IEnumerable data)
- where TEntity : class
- {
- foreach (var entity in data)
- {
- yield return entity;
- }
+ private static void ConfigureAsyncEnumerableCalls(
+ this DbSet mock,
+ IAsyncEnumerable enumerable) where TEntity : class
+ {
+ mock.GetAsyncEnumerator(Arg.Any()).Returns(args => enumerable.GetAsyncEnumerator());
+ }
+
+ private static void ConfigureDbSetCalls(this DbSet mock, IQueryable data)
+ where TEntity : class
+ {
+ mock.AsQueryable().Returns(mock);
+ mock.AsAsyncEnumerable().Returns(args => CreateAsyncMock(data));
+ }
+
+ private static async IAsyncEnumerable CreateAsyncMock(IEnumerable data)
+ where TEntity : class
+ {
+ foreach (var entity in data)
+ {
+ yield return entity;
+ }
- await Task.CompletedTask;
+ await Task.CompletedTask;
+ }
}
- }
}
\ No newline at end of file
diff --git a/src/MockQueryable/MockQueryable.Sample/MyService.cs b/src/MockQueryable/MockQueryable.Sample/MyService.cs
index e64c33c..f258d56 100644
--- a/src/MockQueryable/MockQueryable.Sample/MyService.cs
+++ b/src/MockQueryable/MockQueryable.Sample/MyService.cs
@@ -93,6 +93,8 @@ public interface IUserRepository
Task> GetAll();
+ Task> GetAllAsQueryable();
+
IAsyncEnumerable GetAllAsync();
Task DeleteUserAsync(Guid id);
diff --git a/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs b/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs
index bba01fc..647f4db 100644
--- a/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs
+++ b/src/MockQueryable/MockQueryable.Sample/MyServiceFakeItEasyTests.cs
@@ -85,32 +85,6 @@ public async Task GetUserReports_AutoMap(DateTime from, DateTime to, int expecte
Assert.That(expectedCount, Is.EqualTo(result.Count));
}
- [TestCase("AnyFirstName", "AnyExistLastName", "01/20/2012", "Users with DateOfBirth more than limit")]
- [TestCase("ExistFirstName", "AnyExistLastName", "02/20/2012", "User with FirstName already exist")]
- [TestCase("AnyFirstName", "ExistLastName", "01/20/2012", "User already exist")]
- public void DbSetCreateUserIfNotExist(string firstName, string lastName, DateTime dateOfBirth, string expectedError)
- {
- // arrange
- var users = new List
- {
- new() {LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat)},
- new() {FirstName = "ExistFirstName"},
- new() {DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat)},
- new() {DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat)},
- new() {DateOfBirth = DateTime.Parse("01/20/2012", UsCultureInfo.DateTimeFormat)},
- };
- var mock = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mock);
- var service = new MyService(userRepository);
-
- // act
- var ex = Assert.ThrowsAsync(() =>
- service.CreateUserIfNotExist(firstName, lastName, dateOfBirth));
-
- // assert
- Assert.That(expectedError, Is.EqualTo(ex.Message));
- }
-
[TestCase("AnyFirstName", "AnyExistLastName", "01/20/2012", "Users with DateOfBirth more than limit")]
[TestCase("ExistFirstName", "AnyExistLastName", "02/20/2012", "User with FirstName already exist")]
[TestCase("AnyFirstName", "ExistLastName", "01/20/2012", "User already exist")]
@@ -135,31 +109,6 @@ public void DbSetCreatedFromCollectionCreateUserIfNotExist(string firstName, str
Assert.That(expectedError, Is.EqualTo(ex.Message));
}
- [TestCase("AnyFirstName", "ExistLastName", "01/20/2012")]
- public async Task DbSetCreateUser(string firstName, string lastName, DateTime dateOfBirth)
- {
- // arrange
- var userEntities = new List();
- var mock = userEntities.BuildMockDbSet();
- A.CallTo(() => mock.AddAsync(A._, A._))
- .ReturnsLazily(call =>
- {
- userEntities.Add((UserEntity)call.Arguments[0]);
- return default;
- });
- var userRepository = new TestDbSetRepository(mock);
- var service = new MyService(userRepository);
-
- // act
- await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth);
-
- // assert
- var entity = mock.Single();
- Assert.That(firstName, Is.EqualTo(entity.FirstName));
- Assert.That(lastName, Is.EqualTo(entity.LastName));
- Assert.That(dateOfBirth, Is.EqualTo(entity.DateOfBirth));
- }
-
[TestCase("AnyFirstName", "ExistLastName", "01/20/2012")]
public async Task DbSetCreatedFromCollectionCreateUser1(string firstName, string lastName, DateTime dateOfBirth)
{
@@ -179,31 +128,12 @@ public async Task DbSetCreatedFromCollectionCreateUser1(string firstName, string
await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth);
// assert
- var entity = mock.Single();
+ var entity = await mock.SingleAsync();
Assert.That(firstName, Is.EqualTo(entity.FirstName));
Assert.That(lastName, Is.EqualTo(entity.LastName));
Assert.That(dateOfBirth, Is.EqualTo(entity.DateOfBirth));
}
- [TestCase("01/20/2012", "06/20/2018", 5)]
- [TestCase("01/20/2012", "06/20/2012", 4)]
- [TestCase("01/20/2012", "02/20/2012", 3)]
- [TestCase("01/20/2010", "02/20/2011", 0)]
- public async Task DbSetGetUserReports(DateTime from, DateTime to, int expectedCount)
- {
- // arrange
- var users = TestDataHelper.CreateUserList();
- var mock = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mock);
- var service = new MyService(userRepository);
-
- // act
- var result = await service.GetUserReports(from, to);
-
- // assert
- Assert.That(expectedCount, Is.EqualTo(result.Count));
- }
-
[TestCase("01/20/2012", "06/20/2018", 5)]
[TestCase("01/20/2012", "06/20/2012", 4)]
[TestCase("01/20/2012", "02/20/2012", 3)]
@@ -223,8 +153,8 @@ public async Task DbSetCreatedFromCollectionGetUserReports(DateTime from, DateTi
Assert.That(expectedCount, Is.EqualTo(result.Count));
}
- [TestCase]
- public async Task DbSetGetAllUserEntitiesAsync()
+ [Test]
+ public async Task DbSetCreatedFromCollectionGetAllUserEntitiesAsync()
{
// arrange
var users = TestDataHelper.CreateUserList();
@@ -239,22 +169,6 @@ public async Task DbSetGetAllUserEntitiesAsync()
Assert.That(users.Count, Is.EqualTo(result.Count));
}
- [Test]
- public async Task DbSetCreatedFromCollectionGetAllUserEntitiesAsync()
- {
- // arrange
- var users = TestDataHelper.CreateUserList();
-
- var mockDbSet = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mockDbSet);
-
- // act
- var result = await userRepository.GetAllAsync().ToListAsync();
-
- // assert
- Assert.That(users.Count, Is.EqualTo(result.Count));
- }
-
[Test]
public async Task DbSetGetAllUserEntitiesAsync_ShouldReturnAllEntities_WhenSourceIsChanged()
{
@@ -275,21 +189,6 @@ public async Task DbSetGetAllUserEntitiesAsync_ShouldReturnAllEntities_WhenSourc
Assert.That(users.Count, Is.EqualTo(result2.Count));
}
- [Test]
- public async Task DbSetGetAllUserEntity()
- {
- //arrange
- var users = TestDataHelper.CreateUserList();
- var mock = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mock);
-
- //act
- var result = await userRepository.GetAll();
-
- //assert
- Assert.That(users.Count, Is.EqualTo(result.Count));
- }
-
[Test]
public async Task DbSetCreatedFromCollectionGetAllUserEntity()
{
@@ -305,7 +204,7 @@ public async Task DbSetCreatedFromCollectionGetAllUserEntity()
Assert.That(users.Count, Is.EqualTo(result.Count));
}
- [TestCase]
+ [Test]
public void GetUsersByFirstName_ExpressionVisitorMissing_ThrowsException()
{
// arrange
@@ -326,7 +225,7 @@ public void GetUsersByFirstName_ExpressionVisitorMissing_ThrowsException()
"Rewrite the query to avoid client evaluation of arguments so that method can be translated to server."));
}
- [TestCase]
+ [Test]
public async Task GetUsersByFirstName_PartOfNameCaseInsensitiveSearch_AllMatchesReturned()
{
// arrange
@@ -342,7 +241,7 @@ public async Task GetUsersByFirstName_PartOfNameCaseInsensitiveSearch_AllMatches
Assert.That(result.Count, Is.EqualTo(2));
}
- [TestCase]
+ [Test]
public void GetUsersByLastName_ExpressionVisitorMissing_ThrowsException()
{
// arrange
@@ -363,7 +262,7 @@ public void GetUsersByLastName_ExpressionVisitorMissing_ThrowsException()
"Rewrite the query to avoid client evaluation of arguments so that method can be translated to server."));
}
- [TestCase]
+ [Test]
public async Task GetUsersByLastName_PartOfNameCaseInsensitiveSearch_AllMatchesReturned()
{
// arrange
@@ -379,42 +278,56 @@ public async Task GetUsersByLastName_PartOfNameCaseInsensitiveSearch_AllMatchesR
Assert.That(result.Count, Is.EqualTo(3));
}
+ [Test]
+ public async Task GetAllUsers_AsQueryableList_AllMatchesReturned()
+ {
+ // arrange
+ var users = TestDataHelper.CreateUserList();
+
+ var mockDbSet = users.BuildMockDbSet();
+ var userRepository = new TestDbSetRepository(mockDbSet);
+
+ // act
+ var result = await userRepository.GetAllAsQueryable();
+ // assert
+ Assert.That(users.Count, Is.EqualTo(result.Count()));
+ }
[Test]
public async Task DbSetCreatedFromCollection_ExecuteDeleteAsync()
{
- // arrange
- var userId = Guid.NewGuid();
- var users = TestDataHelper.CreateUserList(userId);
+ // arrange
+ var userId = Guid.NewGuid();
+ var users = TestDataHelper.CreateUserList(userId);
- var mockDbSet = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mockDbSet);
+ var mockDbSet = users.BuildMockDbSet();
+ var userRepository = new TestDbSetRepository(mockDbSet);
- // act
- var count = await userRepository.DeleteUserAsync(userId);
+ // act
+ var count = await userRepository.DeleteUserAsync(userId);
- // assert
- Assert.That(count, Is.EqualTo(1));
- var updatedUsers = await userRepository.GetAllAsync().ToListAsync();
- Assert.That(updatedUsers.Any(x => x.Id == userId), Is.EqualTo(false));
+ // assert
+ Assert.That(count, Is.EqualTo(1));
+ var updatedUsers = await userRepository.GetAllAsync().ToListAsync();
+ Assert.That(updatedUsers.Any(x => x.Id == userId), Is.EqualTo(false));
}
[Test]
public async Task DbSetCreatedFromCollectionExecuteDeleteAsync_ShouldReturnZero()
{
- // arrange
- var userId = Guid.NewGuid();
- var users = TestDataHelper.CreateUserList(userId);
+ // arrange
+ var userId = Guid.NewGuid();
+ var users = TestDataHelper.CreateUserList(userId);
- var mockDbSet = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mockDbSet);
+ var mockDbSet = users.BuildMockDbSet();
+ var userRepository = new TestDbSetRepository(mockDbSet);
- //act
- var count = await userRepository.DeleteUserAsync(Guid.NewGuid());
+ //act
+ var count = await userRepository.DeleteUserAsync(Guid.NewGuid());
- // assert
- Assert.That(count, Is.EqualTo(0));
+ // assert
+ Assert.That(count, Is.EqualTo(0));
}
[Test]
@@ -434,8 +347,8 @@ public async Task DbSetCreatedFromCollectionExecuteUpdateAsync()
//assert
Assert.That(count, Is.EqualTo(1));
var user = users.Single(x => x.Id == userId);
- Assert.That(expectedName, Is.EqualTo(user.FirstName));
- Assert.That(expectedName, Is.EqualTo(user.LastName));
+ Assert.That(expectedName, Is.EqualTo(user.FirstName));
+ Assert.That(expectedName, Is.EqualTo(user.LastName));
}
[Test]
@@ -457,10 +370,10 @@ public async Task DbSetCreatedFromCollectionExecuteUpdateAsync_ShouldReturnZero(
Assert.That(count, Is.EqualTo(0));
var user = users.Single(x => x.Id == userId);
- Assert.That(expectedFirstName, Is.EqualTo(user.FirstName));
- Assert.That(expectedLastName, Is.EqualTo(user.LastName));
+ Assert.That(expectedFirstName, Is.EqualTo(user.FirstName));
+ Assert.That(expectedLastName, Is.EqualTo(user.LastName));
}
-
+
}
\ No newline at end of file
diff --git a/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs b/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs
index d9e3b1f..19ed493 100644
--- a/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs
+++ b/src/MockQueryable/MockQueryable.Sample/MyServiceMoqTests.cs
@@ -13,8 +13,6 @@ namespace MockQueryable.Sample;
[TestFixture]
public class MyServiceMoqTests
{
-
-
[TestCase("AnyFirstName", "AnyExistLastName", "01/20/2012", "Users with DateOfBirth more than limit")]
[TestCase("ExistFirstName", "AnyExistLastName", "02/20/2012", "User with FirstName already exist")]
[TestCase("AnyFirstName", "ExistLastName", "01/20/2012", "User already exist")]
@@ -137,26 +135,6 @@ public void DbSetCreatedFromCollectionCreateUserIfNotExist(string firstName, str
Assert.That(expectedError, Is.EqualTo(ex.Message));
}
- [TestCase("AnyFirstName", "ExistLastName", "01/20/2012")]
- public async Task DbSetCreateUser(string firstName, string lastName, DateTime dateOfBirth)
- {
- //arrange
- var userEntities = new List();
- var mock = userEntities.BuildMockDbSet();
-
- mock.Setup(set => set.AddAsync(It.IsAny(), It.IsAny()))
- .Callback((UserEntity entity, CancellationToken _) => userEntities.Add(entity));
- var userRepository = new TestDbSetRepository(mock.Object);
- var service = new MyService(userRepository);
- //act
- await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth);
- // assert
- var entity = mock.Object.Single();
- Assert.That(firstName, Is.EqualTo(entity.FirstName));
- Assert.That(lastName, Is.EqualTo(entity.LastName));
- Assert.That(dateOfBirth, Is.EqualTo(entity.DateOfBirth));
- }
-
[TestCase("AnyFirstName", "ExistLastName", "01/20/2012")]
public async Task DbSetCreatedFromCollectionCreateUser(string firstName, string lastName, DateTime dateOfBirth)
{
@@ -171,29 +149,12 @@ public async Task DbSetCreatedFromCollectionCreateUser(string firstName, string
//act
await service.CreateUserIfNotExist(firstName, lastName, dateOfBirth);
// assert
- var entity = mock.Object.Single();
+ var entity = await mock.Object.SingleAsync();
Assert.That(firstName, Is.EqualTo(entity.FirstName));
Assert.That(lastName, Is.EqualTo(entity.LastName));
Assert.That(dateOfBirth, Is.EqualTo(entity.DateOfBirth));
}
- [TestCase("01/20/2012", "06/20/2018", 5)]
- [TestCase("01/20/2012", "06/20/2012", 4)]
- [TestCase("01/20/2012", "02/20/2012", 3)]
- [TestCase("01/20/2010", "02/20/2011", 0)]
- public async Task DbSetGetUserReports(DateTime from, DateTime to, int expectedCount)
- {
- //arrange
- var users = TestDataHelper.CreateUserList();
- var mock = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mock.Object);
- var service = new MyService(userRepository);
- //act
- var result = await service.GetUserReports(from, to);
- //assert
- Assert.That(expectedCount, Is.EqualTo(result.Count));
- }
-
[TestCase("01/20/2012", "06/20/2018", 5)]
[TestCase("01/20/2012", "06/20/2012", 4)]
[TestCase("01/20/2012", "02/20/2012", 3)]
@@ -211,19 +172,6 @@ public async Task DbSetCreatedFromCollectionGetUserReports(DateTime from, DateTi
Assert.That(expectedCount, Is.EqualTo(result.Count));
}
- [TestCase]
- public async Task DbSetGetAllUserEntity()
- {
- //arrange
- var users = TestDataHelper.CreateUserList();
- var mock = users.BuildMockDbSet();
- var userRepository = new TestDbSetRepository(mock.Object);
- //act
- var result = await userRepository.GetAll();
- //assert
- Assert.That(users.Count, Is.EqualTo(result.Count));
- }
-
[TestCase]
public async Task DbSetCreatedFromCollectionGetAllUserEntity()
{
@@ -237,29 +185,6 @@ public async Task DbSetCreatedFromCollectionGetAllUserEntity()
Assert.That(users.Count, Is.EqualTo(result.Count));
}
- [TestCase]
- public async Task DbSetFindAsyncUserEntity()
- {
- //arrange
- var userId = Guid.NewGuid();
- var users = TestDataHelper.CreateUserList(userId);
-
- var mock = users.BuildMockDbSet();
- mock.Setup(x => x.FindAsync(It.IsAny