Files
vegasco/tests/Vegasco.Server.Api.Tests.Integration/Cars/GetCarsTests.cs
ThompsonNye ab32be98a6
All checks were successful
continuous-integration/drone/push Build is passing
Use concrete types
2025-06-24 19:28:55 +02:00

67 lines
1.8 KiB
C#

using FluentAssertions;
using System.Net;
using System.Net.Http.Json;
using Vegasco.Server.Api.Cars;
namespace Vegasco.Server.Api.Tests.Integration.Cars;
[Collection(SharedTestCollection.Name)]
public class GetCarsTests : IAsyncLifetime
{
private readonly WebAppFactory _factory;
private readonly CarFaker _carFaker = new();
public GetCarsTests(WebAppFactory factory)
{
_factory = factory;
}
[Fact]
public async Task GetCars_ShouldReturnEmptyList_WhenNoEntriesExist()
{
// Arrange
// Act
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/cars");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
GetCars.ApiResponse? apiResponse = await response.Content.ReadFromJsonAsync<GetCars.ApiResponse>();
apiResponse!.Cars.Should().BeEmpty();
}
[Fact]
public async Task GetCars_ShouldReturnEntries_WhenEntriesExist()
{
// Arrange
List<CreateCar.Response> createdCars = [];
const int numberOfCars = 5;
for (int i = 0; i < numberOfCars; i++)
{
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
createCarResponse.EnsureSuccessStatusCode();
CreateCar.Response? createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
createdCars.Add(createdCar!);
}
// Act
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/cars");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
GetCars.ApiResponse? apiResponse = await response.Content.ReadFromJsonAsync<GetCars.ApiResponse>();
apiResponse!.Cars.Should().BeEquivalentTo(createdCars);
}
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _factory.ResetDatabaseAsync();
}
}