Files

58 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-08-17 16:38:40 +02:00
using FluentAssertions;
using System.Net;
using System.Net.Http.Json;
using Vegasco.Server.Api.Cars;
2024-08-17 16:38:40 +02:00
namespace Vegasco.Server.Api.Tests.Integration.Cars;
2024-08-17 16:38:40 +02:00
[Collection(SharedTestCollection.Name)]
public class GetCarTests : IAsyncLifetime
{
private readonly WebAppFactory _factory;
private readonly CarFaker _carFaker = new();
public GetCarTests(WebAppFactory factory)
{
_factory = factory;
}
[Fact]
public async Task GetCar_ShouldReturnNotFound_WhenCarDoesNotExist()
{
// Arrange
2025-06-24 19:28:55 +02:00
Guid randomCarId = Guid.NewGuid();
2024-08-17 16:38:40 +02:00
// Act
HttpResponseMessage response = await _factory.HttpClient.GetAsync($"v1/cars/{randomCarId}");
2024-08-17 16:38:40 +02:00
// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
[Fact]
public async Task GetCar_ShouldReturnCar_WhenCarExists()
{
// Arrange
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
2024-08-17 16:38:40 +02:00
createCarResponse.EnsureSuccessStatusCode();
2025-06-24 19:28:55 +02:00
CreateCar.Response? createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
2024-08-17 16:38:40 +02:00
// Act
HttpResponseMessage response = await _factory.HttpClient.GetAsync($"v1/cars/{createdCar!.Id}");
2024-08-17 16:38:40 +02:00
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
2025-06-24 19:28:55 +02:00
GetCar.Response? car = await response.Content.ReadFromJsonAsync<GetCar.Response>();
2024-08-17 16:38:40 +02:00
car.Should().BeEquivalentTo(createdCar);
}
public Task InitializeAsync() => Task.CompletedTask;
public async Task DisposeAsync()
{
await _factory.ResetDatabaseAsync();
}
}