test(CustomersControllerTests.cs): update test method name from Should_ReturnsHttpStatus200Ok_When_GetById to Should_ReturnsHttpStatus200Ok_When_GetById_ValidRequest

test(CustomersControllerTests.cs): add new test Should_eturnsHttpStatus400BadRequest_When_GetById_InvalidRequest
test(CustomersControllerTests.cs): add new test Should_ReturnsStatus404NotFound_When_GetById_NonExistingCustomer
This commit is contained in:
Jean Gatto
2023-08-27 16:33:38 -03:00
parent b67c58f566
commit 304e816a16
@@ -161,7 +161,7 @@ public class CustomersControllerTests : IAsyncLifetime
}
[Fact]
public async Task Should_ReturnsHttpStatus200Ok_When_GetById()
public async Task Should_ReturnsHttpStatus200Ok_When_GetById_ValidRequest()
{
// Arrange
var queryModel = new Faker<CustomerQueryModel>()
@@ -186,7 +186,7 @@ public class CustomersControllerTests : IAsyncLifetime
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
// Act
using var act = await httpClient.GetAsync($"/api/customers/{queryModel.Id}");
using var act = await httpClient.GetAsync($"{Endpoint}/{queryModel.Id}");
// Assert (HTTP)
act.Should().NotBeNull();
@@ -211,6 +211,80 @@ public class CustomersControllerTests : IAsyncLifetime
await readOnlyRepository.Received(1).GetByIdAsync(Arg.Is<Guid>(id => id == queryModel.Id));
}
[Fact]
public async Task Should_eturnsHttpStatus400BadRequest_When_GetById_InvalidRequest()
{
// Arrange
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
readOnlyRepository.GetByIdAsync(Arg.Any<Guid>()).Returns((CustomerQueryModel)null);
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
{
services.RemoveAll<ICustomerReadOnlyRepository>();
services.AddScoped(_ => readOnlyRepository);
});
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
var customerId = Guid.Empty;
// Act
using var act = await httpClient.GetAsync($"{Endpoint}/{customerId}");
// Assert (HTTP)
act.Should().NotBeNull();
act.IsSuccessStatusCode.Should().BeFalse();
act.StatusCode.Should().Be(HttpStatusCode.BadRequest);
// Assert (HTTP Content Response)
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse<CustomerQueryModel>>();
response.Should().NotBeNull();
response.Success.Should().BeFalse();
response.StatusCode.Should().Be(StatusCodes.Status400BadRequest);
response.Result.Should().BeNull();
response.Errors.Should().NotBeNullOrEmpty().And.OnlyHaveUniqueItems();
await readOnlyRepository.DidNotReceive().GetByIdAsync(Arg.Is<Guid>(id => id == customerId));
}
[Fact]
public async Task Should_ReturnsStatus404NotFound_When_GetById_NonExistingCustomer()
{
// Arrange
var readOnlyRepository = Substitute.For<ICustomerReadOnlyRepository>();
readOnlyRepository.GetByIdAsync(Arg.Any<Guid>()).Returns((CustomerQueryModel)null);
await using var webApplicationFactory = InitializeWebAppFactory(configureServices: (services) =>
{
services.RemoveAll<ICustomerReadOnlyRepository>();
services.AddScoped(_ => readOnlyRepository);
});
using var httpClient = webApplicationFactory.CreateClient(CreateClientOptions());
var customerId = Guid.NewGuid();
// Act
using var act = await httpClient.GetAsync($"{Endpoint}/{customerId}");
// Assert (HTTP)
act.Should().NotBeNull();
act.IsSuccessStatusCode.Should().BeFalse();
act.StatusCode.Should().Be(HttpStatusCode.NotFound);
// Assert (HTTP Content Response)
var response = (await act.Content.ReadAsStringAsync()).FromJson<ApiResponse<CustomerQueryModel>>();
response.Should().NotBeNull();
response.Success.Should().BeFalse();
response.StatusCode.Should().Be(StatusCodes.Status404NotFound);
response.Result.Should().BeNull();
response.Errors.Should().NotBeNullOrEmpty()
.And.OnlyHaveUniqueItems()
.And.AllSatisfy(error => error.Message.Should().Be($"No customer found by Id: {customerId}"));
await readOnlyRepository.Received(1).GetByIdAsync(Arg.Is<Guid>(id => id == customerId));
}
#region IAsyncLifetime
public async Task InitializeAsync()