Skip to content
Permalink
Browse files

Added FakeItEasy Extension methods and Unit tests.

Added CultureInfo for correct DateTime parsing
  • Loading branch information...
BartDM committed Jul 23, 2019
1 parent 113dfd0 commit ab7da3e66aeb25095b5342e5e14d5f6cbb8f34fc
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.Linq;
using FakeItEasy;
using Microsoft.EntityFrameworkCore;

namespace MockQueryable.FakeItEasy
{
public static class FakeItEasyExtensions
{
public static IQueryable<TEntity> BuildMock<TEntity>(this IQueryable<TEntity> data) where TEntity : class
{
var mock = A.Fake<IQueryable<TEntity>>(d=>d.Implements<IAsyncEnumerable<TEntity>>().Implements<IQueryable<TEntity>>());
var enumerable = new TestAsyncEnumerable<TEntity>(data);
A.CallTo(() => ((IAsyncEnumerable<TEntity>) mock).GetEnumerator()).Returns(enumerable.GetEnumerator());
A.CallTo(() => ((IQueryable<TEntity>)mock).Provider).Returns(enumerable);
A.CallTo(() => ((IQueryable<TEntity>)mock).Expression).Returns(data?.Expression);
A.CallTo(() => ((IQueryable<TEntity>)mock).ElementType).Returns(data?.ElementType);
A.CallTo(() => ((IQueryable<TEntity>)mock).GetEnumerator()).Returns(data?.GetEnumerator());
return mock;
}

public static DbSet<TEntity> BuildMockDbSet<TEntity>(this IQueryable<TEntity> data) where TEntity : class
{
var mock = A.Fake<DbSet<TEntity>>(d => d.Implements<IAsyncEnumerable<TEntity>>().Implements<IQueryable<TEntity>>());
var enumerable = new TestAsyncEnumerable<TEntity>(data);
A.CallTo(() => ((IAsyncEnumerable<TEntity>)mock).GetEnumerator()).Returns(enumerable.GetEnumerator());
A.CallTo(() => ((IQueryable<TEntity>)mock).Provider).Returns(enumerable);
A.CallTo(() => ((IQueryable<TEntity>)mock).Expression).Returns(data?.Expression);
A.CallTo(() => ((IQueryable<TEntity>)mock).ElementType).Returns(data?.ElementType);
A.CallTo(() => ((IQueryable<TEntity>)mock).GetEnumerator()).Returns(data?.GetEnumerator());
return mock;
}

public static DbQuery<TEntity> BuildMockDbQuery<TEntity>(this IQueryable<TEntity> data) where TEntity : class
{
var mock = A.Fake<DbQuery<TEntity>>(d => d.Implements<IAsyncEnumerable<TEntity>>().Implements<IQueryable<TEntity>>());
var enumerable = new TestAsyncEnumerable<TEntity>(data);
A.CallTo(() => ((IAsyncEnumerable<TEntity>)mock).GetEnumerator()).Returns(enumerable.GetEnumerator());
A.CallTo(() => ((IQueryable<TEntity>)mock).Provider).Returns(enumerable);
A.CallTo(() => ((IQueryable<TEntity>)mock).Expression).Returns(data?.Expression);
A.CallTo(() => ((IQueryable<TEntity>)mock).ElementType).Returns(data?.ElementType);
A.CallTo(() => ((IQueryable<TEntity>)mock).GetEnumerator()).Returns(data?.GetEnumerator());
return mock;
}
}
}
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="5.1.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MockQueryable\MockQueryable.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Interactive.Async">
<HintPath>..\..\..\..\..\..\.nuget\packages\system.interactive.async\3.2.0\lib\netstandard2.0\System.Interactive.Async.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
@@ -6,6 +6,7 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="FakeItEasy" Version="5.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="Moq" Version="4.8.3" />
@@ -15,6 +16,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MockQueryable.FakeItEasy\MockQueryable.FakeItEasy.csproj" />
<ProjectReference Include="..\MockQueryable.Moq\MockQueryable.Moq.csproj" />
<ProjectReference Include="..\MockQueryable.NSubstitute\MockQueryable.NSubstitute.csproj" />
<ProjectReference Include="..\MockQueryable\MockQueryable.csproj" />
@@ -0,0 +1,196 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using FakeItEasy;
using MockQueryable.FakeItEasy;
using Moq;
using NUnit.Framework;

namespace MockQueryable.Sample
{
[TestFixture]
public class MyServiceFakeItEasyTests
{
private static readonly CultureInfo UsCultureInfo = new CultureInfo("en-US");

[OneTimeSetUp]
public void SetUp()
{
MyService.Initialize();
}

[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 CreateUserIfNotExist(string firstName, string lastName, DateTime dateOfBirth, string expectedError)
{
//arrange

var userRepository = A.Fake<IUserRepository>();
//var userRepository = new Mock<IUserRepository>();
var service = new MyService(userRepository);
var users = new List<UserEntity>
{
new UserEntity{LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "ExistFirstName"},
new UserEntity{DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
};
//expect
var mock = users.AsQueryable().BuildMock();
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);
//act
var ex = Assert.ThrowsAsync<ApplicationException>(() => service.CreateUserIfNotExist(firstName, lastName, dateOfBirth));
//assert
Assert.AreEqual(expectedError, ex.Message);

}

[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 GetUserReports(DateTime from, DateTime to, int expectedCount)
{
//arrange
var userRepository = A.Fake<IUserRepository>();
var service = new MyService(userRepository);
var users = new List<UserEntity>
{
new UserEntity{FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018",UsCultureInfo.DateTimeFormat)},
};
//expect
var mock = users.AsQueryable().BuildMock();
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);
//act
var result = await service.GetUserReports(from, to);
//assert
Assert.AreEqual(expectedCount, 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)]
[TestCase("01/20/2010", "02/20/2011", 0)]
public async Task GetUserReports_DbQuery(DateTime from, DateTime to, int expectedCount)
{
//arrange
var userRepository = A.Fake<IUserRepository>();
var service = new MyService(userRepository);
var users = new List<UserEntity>
{
new UserEntity{FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018",UsCultureInfo.DateTimeFormat)},
};
//expect
var mock = users.AsQueryable().BuildMockDbQuery();
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);
//act
var result = await service.GetUserReports(from, to);
//assert
Assert.AreEqual(expectedCount, 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)]
[TestCase("01/20/2010", "02/20/2011", 0)]
public async Task GetUserReports_AutoMap(DateTime from, DateTime to, int expectedCount)
{
//arrange
var userRepository = A.Fake<IUserRepository>();
var service = new MyService(userRepository);
var users = new List<UserEntity>
{
new UserEntity{FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018",UsCultureInfo.DateTimeFormat)},
};
//expect
var mock = users.AsQueryable().BuildMock();
A.CallTo(() => userRepository.GetQueryable()).Returns(mock);
//act
var result = await service.GetUserReportsAutoMap(from, to);
//assert
Assert.AreEqual(expectedCount, 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<UserEntity>
{
new UserEntity{LastName = "ExistLastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "ExistFirstName"},
new UserEntity{DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
};
var mock = users.AsQueryable().BuildMockDbSet();
var userRepository = new TestDbSetRepository(mock);
var service = new MyService(userRepository);
//act
var ex = Assert.ThrowsAsync<ApplicationException>(() => service.CreateUserIfNotExist(firstName, lastName, dateOfBirth));
//assert
Assert.AreEqual(expectedError, ex.Message);

}

[TestCase("AnyFirstName", "ExistLastName", "01/20/2012")]
public async Task DbSetCreateUser(string firstName, string lastName, DateTime dateOfBirth)
{
//arrange
var mock = new List<UserEntity>().AsQueryable().BuildMockDbSet();
var userRepository = new TestDbSetRepository(mock);
var service = new MyService(userRepository);
//act
await service.CreateUserIfNotExist(firstName, lastName, 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 = new List<UserEntity>
{
new UserEntity{FirstName = "FirstName1", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName2", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName3", LastName = "LastName", DateOfBirth = DateTime.Parse("03/20/2012",UsCultureInfo.DateTimeFormat)},
new UserEntity{FirstName = "FirstName5", LastName = "LastName", DateOfBirth = DateTime.Parse("01/20/2018",UsCultureInfo.DateTimeFormat)},
};
var mock = users.AsQueryable().BuildMockDbSet();
var userRepository = new TestDbSetRepository(mock);
var service = new MyService(userRepository);
//act
var result = await service.GetUserReports(from, to);
//assert
Assert.AreEqual(expectedCount, result.Count);
}


}
}

0 comments on commit ab7da3e

Please sign in to comment.
You can’t perform that action at this time.