Skip to content

Commit

Permalink
Unit tests added.
Browse files Browse the repository at this point in the history
  • Loading branch information
TanvirAgoda authored and Tanvir Ahmad Arjel committed Nov 19, 2023
1 parent 6d72934 commit 3ad7c26
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 12 deletions.
11 changes: 11 additions & 0 deletions tests/EFCore.QueryRepository.Tests/DemoDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,16 @@ public class Employee
{
public int Id { get; set; }

public int DepartmentId { get; set; }

public string Name { get; set; }

public Department Department { get; set; }
}

public class Department
{
public int Id { get; set; }

public string Name { get; set; }
}
55 changes: 43 additions & 12 deletions tests/EFCore.QueryRepository.Tests/QueryRepositoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,36 @@ namespace EFCore.QueryRepository.Tests;

public class QueryRepositoryTests
{
private readonly List<Employee> fakeEmployees = new()
{
new()
{
Id = 1,
DepartmentId = 1,
Name = "Mark",
Department = new Department
{
Id = 1,
Name = "IT"
}
},
new()
{
Id = 2,
DepartmentId = 1,
Name = "Merry",
Department = new Department
{
Id = 2,
Name = "HR"
}
}
};

[Fact]
public async Task GetListAsyncTest()
{
// Arrange
var fakeEmployees = new List<Employee>()
{
new() {Id = 1,Name = "Mark"},
new() {Id = 1, Name = "Merry"}
};

Mock<DemoDbContext> mockDemoContext = new();
mockDemoContext.Setup(context => context.Set<Employee>()).ReturnsDbSet(fakeEmployees);

Expand All @@ -34,12 +54,6 @@ public async Task GetListAsyncTest()
public async Task GetListAsync_WithCondition_Test()
{
// Arrange
var fakeEmployees = new List<Employee>()
{
new() {Id = 1,Name = "Mark"},
new() {Id = 2, Name = "Merry"}
};

int expectedCount = fakeEmployees.Where(e => e.Id == 1).Count();
string expectedName = "Mark";

Expand All @@ -56,4 +70,21 @@ public async Task GetListAsync_WithCondition_Test()
Assert.True(employees.Count == expectedCount);
Assert.Equal(expectedName, employees.First().Name);
}

[Fact]
public async Task GetListAsync_WithInclude_Test()
{
// Arrange
Mock<DemoDbContext> mockDemoContext = new();
mockDemoContext.Setup(context => context.Set<Employee>()).ReturnsDbSet(fakeEmployees);

// Act
QueryRepository<DemoDbContext> queryRepository = new(mockDemoContext.Object);
List<Employee> employees = await queryRepository.GetListAsync<Employee>(q => q.Include(e => e.Department));

// Assert
Assert.NotNull(employees);
Assert.NotEmpty(employees);
Assert.True(employees.Count == fakeEmployees.Count);
}
}

0 comments on commit 3ad7c26

Please sign in to comment.