Skip to content

Commit

Permalink
rename and organise tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Apr 28, 2024
1 parent 9b7d9e7 commit 3b2f276
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 156 deletions.
31 changes: 31 additions & 0 deletions CleanAspCore.Api.Tests/Features/Departments/AddDepartmentsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using CleanAspCore.Features.Departments;
using CleanAspCore.Features.Import;

namespace CleanAspCore.Api.Tests.Features.Departments;

public class AddDepartmentsTests : TestBase
{
[Test]
public async Task CreateDepartment_IsAdded()
{
//Arrange
var department = new CreateDepartmentRequestFaker().Generate();

//Act
var response = await Sut.CreateClientFor<IDepartmentApiClient>().CreateDepartment(department);

//Assert
await response.AssertStatusCode(HttpStatusCode.Created);
var createdId = response.GetGuidFromLocationHeader();
Sut.AssertDatabase(context =>
{
context.Departments.Should().BeEquivalentTo(new[]
{
new
{
Id = createdId
}
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CleanAspCore.Api.Tests.Features.Departments;

public class DepartmentControllerTests : TestBase
public class GetDepartmentByIdTests : TestBase
{
[Test]
public async Task GetDepartmentById_ReturnsExpectedDepartment()
Expand All @@ -28,28 +28,4 @@ public async Task GetDepartmentById_ReturnsExpectedDepartment()
Id = department.Id
});
}

[Test]
public async Task CreateDepartment_IsAdded()
{
//Arrange
var department = new CreateDepartmentRequestFaker().Generate();

//Act
var response = await Sut.CreateClientFor<IDepartmentApiClient>().CreateDepartment(department);

//Assert
await response.AssertStatusCode(HttpStatusCode.Created);
var createdId = response.GetGuidFromLocationHeader();
Sut.AssertDatabase(context =>
{
context.Departments.Should().BeEquivalentTo(new[]
{
new
{
Id = createdId
}
});
});
}
}
30 changes: 30 additions & 0 deletions CleanAspCore.Api.Tests/Features/Employees/CreateEmployeeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CleanAspCore.Features.Employees;
using CleanAspCore.Features.Import;

namespace CleanAspCore.Api.Tests.Features.Employees;

public class CreateEmployeeTests : TestBase
{
[Test]
public async Task CreateEmployee_IsAdded()
{
//Arrange
var createEmployeeRequest = new CreateEmployeeRequestFaker().Generate();
Sut.SeedData(context =>
{
context.Departments.Add(new DepartmentFaker().RuleFor(x => x.Id, createEmployeeRequest.DepartmentId).Generate());
context.Jobs.Add(new JobFaker().RuleFor(x => x.Id, createEmployeeRequest.JobId).Generate());
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().CreateEmployee(createEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.Created);
var createdId = response.GetGuidFromLocationHeader();
Sut.AssertDatabase(context =>
{
context.Employees.Should().BeEquivalentTo(new[] { new { Id = createdId } });
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CleanAspCore.Features.Employees;
using CleanAspCore.Features.Import;

namespace CleanAspCore.Api.Tests.Features.Employees;

public class DeleteEmployeeByIdTests : TestBase
{
[Test]
public async Task DeleteEmployeeById_IsDeleted()
{
//Arrange
var employee = new EmployeeFaker().Generate();
Sut.SeedData(context =>
{
context.Employees.Add(employee);
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().DeleteEmployeeById(employee.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.OK);
Sut.AssertDatabase(context => { context.Employees.Should().BeEmpty(); });
}
}
107 changes: 0 additions & 107 deletions CleanAspCore.Api.Tests/Features/Employees/EmployeeControllerTests.cs

This file was deleted.

27 changes: 27 additions & 0 deletions CleanAspCore.Api.Tests/Features/Employees/GetEmployeeByIdTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CleanAspCore.Features.Employees;
using CleanAspCore.Features.Employees.Endpoints;
using CleanAspCore.Features.Import;

namespace CleanAspCore.Api.Tests.Features.Employees;

public class GetEmployeeByIdTests : TestBase
{
[Test]
public async Task GetEmployeeById_ReturnsExpectedEmployee()
{
//Arrange
var employee = new EmployeeFaker().Generate();
Sut.SeedData(context =>
{
context.Employees.Add(employee);
});

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().GetEmployeeById(employee.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.OK);
var employeeDto = await response.Content.ReadFromJsonAsync<EmployeeDto>();
employeeDto.Should().BeEquivalentTo(new { Id = employee.Id });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CleanAspCore.Features.Employees;
using CleanAspCore.Features.Employees.Endpoints;
using CleanAspCore.Features.Import;

namespace CleanAspCore.Api.Tests.Features.Employees;

public class UpdateEmployeeByIdTests : TestBase
{
[Test]
public async Task UpdateEmployeeById_IsUpdated()
{
//Arrange
var employee = new EmployeeFaker().Generate();
Sut.SeedData(context => { context.Employees.Add(employee); });

UpdateEmployeeRequest updateEmployeeRequest = new()
{
FirstName = "Updated"
};

//Act
var response = await Sut.CreateClientFor<IEmployeeApiClient>().UpdateEmployeeById(employee.Id, updateEmployeeRequest);

//Assert
await response.AssertStatusCode(HttpStatusCode.NoContent);
Sut.AssertDatabase(context =>
{
context.Employees.Should().BeEquivalentTo(new[]
{
new
{
FirstName = "Updated",
LastName = employee.LastName,
}
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CleanAspCore.Api.Tests.Features.Import;

public class ImportControllerTests : TestBase
public class ImportTests : TestBase
{
[Test]
public async Task Import_SingleNewEmployee_IsImported()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,8 @@

namespace CleanAspCore.Api.Tests.Features.Jobs;

public class JobControllerTests : TestBase
public class CreateJobTests : TestBase
{
[Test]
public async Task GetJobById_ReturnsExpectedJob()
{
//Arrange
var job = new JobFaker().Generate();
Sut.SeedData(context =>
{
context.Jobs.Add(job);
});

//Act
var response = await Sut.CreateClientFor<IJobApiClient>().GetJobById(job.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.OK);
var jobDto = await response.Content.ReadFromJsonAsync<JobDto>();
jobDto.Should().BeEquivalentTo(new
{
Id = job.Id
});
}

[Test]
public async Task CreateJob_IsAdded()
{
Expand Down
30 changes: 30 additions & 0 deletions CleanAspCore.Api.Tests/Features/Jobs/GetJobByIdTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using CleanAspCore.Features.Import;
using CleanAspCore.Features.Jobs;
using CleanAspCore.Features.Jobs.Endpoints;

namespace CleanAspCore.Api.Tests.Features.Jobs;

public class GetJobByIdTests : TestBase
{
[Test]
public async Task GetJobById_ReturnsExpectedJob()
{
//Arrange
var job = new JobFaker().Generate();
Sut.SeedData(context =>
{
context.Jobs.Add(job);
});

//Act
var response = await Sut.CreateClientFor<IJobApiClient>().GetJobById(job.Id);

//Assert
await response.AssertStatusCode(HttpStatusCode.OK);
var jobDto = await response.Content.ReadFromJsonAsync<JobDto>();
jobDto.Should().BeEquivalentTo(new
{
Id = job.Id
});
}
}

0 comments on commit 3b2f276

Please sign in to comment.