Skip to content

Commit

Permalink
Implement tests for GetDetails service method
Browse files Browse the repository at this point in the history
  • Loading branch information
oreze committed Jun 3, 2024
1 parent 1382bf8 commit 65e56f6
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public async Task<TripDetailsDto> GetDetails(int id)
.SingleOrDefaultAsync(x => x.ID == id);

if (trip == default)
throw new NotFoundException("Could not find trip with ID {id}.");
throw new NotFoundException($"Could not find trip with ID {id}.");

var registrationsForTrip = trip.Registrations
.Select(x => new RegistrationDetailsDto(x.Email, x.RegisteredAt));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace BackendTripRecruitmentTask.UnitTests.ApplicationTests.ServicesTests;

/// <summary>
/// These tests are not entirely units, but I want to keep them here as a demonstration of how this service could be
/// tested.
/// However this logic should be tested in IntegrationTests project
/// tested.
/// This logic is also tested in IntegrationTests project
/// </summary>
public class TripServiceTests
{
Expand Down Expand Up @@ -407,4 +407,62 @@ public async Task GetByCountry_NoTripsForCountry_ReturnsEmptyList()

Assert.Empty(result);
}

[Fact]
public async Task GetDetails_TripDoesNotExist_ThrowsNotFoundException()
{
var trips = Enumerable.Empty<Trip>();
_mockDbContext.Setup(db => db.Trips)
.ReturnsDbSet(trips);

var exception = await Assert.ThrowsAsync<NotFoundException>(() => _tripService.GetDetails(1));
Assert.Equal("Could not find trip with ID 1.", exception.Message);
_mockDbContext.Verify();
}

[Fact]
public async Task GetDetails_TripExists_ReturnsTripDetailsDto()
{
var country = (Country?)Activator.CreateInstance(typeof(Country), true);
PropertyHelper.SetProperty(country, nameof(Country.Name), "Poland");

var registrations = new List<Registration>();
for (var i = 0; i < 10; i++)
{
var registration = (Registration?)Activator.CreateInstance(typeof(Registration), true);
PropertyHelper.SetProperty(registration, nameof(Registration.Email), $"user{i}@example.com");
PropertyHelper.SetProperty(registration, nameof(Registration.RegisteredAt), DateTime.UtcNow.AddDays(-i));
registrations.Add(registration!);
}

var trip = (Trip?)Activator.CreateInstance(typeof(Trip), true);
PropertyHelper.SetProperty(trip, nameof(Trip.ID), 1);
PropertyHelper.SetProperty(trip, nameof(Trip.Name), "Trip1");
PropertyHelper.SetProperty(trip, nameof(Trip.Country), country!);
PropertyHelper.SetProperty(trip, nameof(Trip.Description), "This is a test trip.");
PropertyHelper.SetProperty(trip, nameof(Trip.StartDate), DateTime.UtcNow.AddDays(1));
PropertyHelper.SetProperty(trip, nameof(Trip.NumberOfSeats), 50);
PropertyHelper.SetProperty(trip, nameof(Trip.Registrations), registrations);

var trips = new List<Trip> { trip! };
_mockDbContext.Setup(db => db.Trips)
.ReturnsDbSet(trips);

var result = await _tripService.GetDetails(1);

Assert.Equal(trip.Name, result.Name);

Check warning on line 453 in BackendTripRecruitmentTask.UnitTests/ApplicationTests/ServicesTests/TripServiceTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 453 in BackendTripRecruitmentTask.UnitTests/ApplicationTests/ServicesTests/TripServiceTests.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Assert.Equal(trip.Country.Name, result.Country);
Assert.Equal(trip.Description, result.Description);
Assert.Equal(trip.StartDate, result.StartDate);
Assert.Equal(trip.NumberOfSeats, result.NumberOfSeats);
Assert.Equal(trip.Registrations.Count, result.RegistrationDetails.Count());
for (var i = 0; i < trip.Registrations.Count; i++)
{
var registration = trip.Registrations[i];
var registrationDto = result.RegistrationDetails.ElementAt(i);
Assert.Equal(registration.Email, registrationDto.Email);
Assert.Equal(registration.RegisteredAt, registrationDto.RegisteredAt);
}
_mockDbContext.Verify();
}
}

0 comments on commit 65e56f6

Please sign in to comment.