Skip to content

Commit

Permalink
try not to use mappers in tests, convert dto's to sealed records.
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed Jul 16, 2023
1 parent 8be4382 commit 188b37d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CleanAspCore.Domain.Departments;
using CleanAspCore.Domain;
using CleanAspCore.Domain.Departments;

namespace CleanAspCore.Api.Tests.Features.Departments;

Expand All @@ -24,7 +25,7 @@ public async Task SearchDepartments_ReturnsExpectedDepartments()
//Assert
result.Should().BeEquivalentTo(new[]
{
department.ToDto()
});
department
}, c => c.ComparingByMembers<Entity>().ExcludingMissingMembers());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CleanAspCore.Domain.Employees;
using CleanAspCore.Domain;
using CleanAspCore.Domain.Employees;

namespace CleanAspCore.Api.Tests.Features.Employees;

Expand All @@ -24,8 +25,8 @@ public async Task SearchEmployee_ReturnsExpectedJobs()
//Assert
result.Should().BeEquivalentTo(new[]
{
employee.ToDto()
});
employee
}, c => c.ComparingByMembers<Entity>().ExcludingMissingMembers());
}

[Fact]
Expand All @@ -38,19 +39,17 @@ public async Task AddEmployee_IsAdded()
context.Departments.Add(employee.Department);
context.Jobs.Add(employee.Job);
});

var newEmployee = employee.ToDto();


//Act
var result = await _api.CreateClient().PostAsJsonAsync("Employee", newEmployee);
var result = await _api.CreateClient().PostAsJsonAsync("Employee", employee.ToDto());

//Assert
result.EnsureSuccessStatusCode();
_api.AssertDatabase(context =>
{
context.Employees.Should().BeEquivalentTo(new[]
{
newEmployee.ToDomain()
employee
});
});
}
Expand Down Expand Up @@ -78,7 +77,7 @@ public async Task UpdateEmployee_IsUpdated()
context.Employees.Should().BeEquivalentTo(new[]
{
updatedEmployee.ToDomain()
}, c => c.ComparingByMembers<Employee>());
});
});
}

Expand Down
5 changes: 3 additions & 2 deletions CleanAspCore.Api.Tests/Features/Jobs/JobControllerTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using CleanAspCore.Domain;
using CleanAspCore.Domain.Jobs;

namespace CleanAspCore.Api.Tests.Features.Jobs;
Expand Down Expand Up @@ -25,7 +26,7 @@ public async Task SearchJobs_ReturnsExpectedJobs()
//Assert
result.Should().BeEquivalentTo(new[]
{
job.ToDto()
});
job
}, c => c.ComparingByMembers<Entity>().ExcludingMissingMembers());
}
}
7 changes: 1 addition & 6 deletions CleanAspCore/Domain/Departments/DepartmentDto.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
namespace CleanAspCore.Domain.Departments;

public class DepartmentDto
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
public sealed record DepartmentDto(int Id, string Name, string City);
10 changes: 1 addition & 9 deletions CleanAspCore/Domain/Employees/EmailAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ public EmailAddress(string email)
Email = email;
Validator.Instance.ValidateAndThrow(this);
}

public static bool operator ==(EmailAddress emailAddress, string email) => emailAddress.Equals(email);

public static bool operator !=(EmailAddress emailAddress, string email) => !emailAddress.Equals(email);

public bool Equals(string email) => Email == email;

public static implicit operator string(EmailAddress emailAddress) => emailAddress.Email;

public static implicit operator EmailAddress(string emailAddress) => new(emailAddress);


public override string ToString() => Email;

private class Validator : AbstractValidator<EmailAddress>
Expand Down
11 changes: 1 addition & 10 deletions CleanAspCore/Domain/Employees/EmployeeDto.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
namespace CleanAspCore.Domain.Employees;

public record class EmployeeDto
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Gender {get; set; }
public int DepartmentId { get; set; }
public int JobId { get; set; }
}
public sealed record EmployeeDto(int Id, string FirstName, string LastName, string Email, string Gender, int DepartmentId, int JobId);
6 changes: 1 addition & 5 deletions CleanAspCore/Domain/Jobs/JobDto.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
namespace CleanAspCore.Domain.Jobs;

public class JobDto
{
public int Id { get; set; }
public string Name { get; set; }
}
public sealed record JobDto(int Id, string Name);

0 comments on commit 188b37d

Please sign in to comment.