Writing the rest of the tests
With all that said, let's write our second test and then hand it over to you to write the rest of them.
Testing that some data exists
The second test we need to write is the Get_ShouldReturnCustomer_WhenCustomerExists
.
For this test we need to create a customer as part of Arrange
, try to retrieve them as part of Act
and then validate that they are returned in the response in Assert
.
We also have to add the customer id in the list so it can get deleted.
Get_ShouldReturnCustomer_WhenCustomerExists
[Fact]
public async Task Get_ShouldReturnCustomer_WhenCustomerExists()
{
// Arrange
var request = new CustomerRequest
{
Email = "[email protected]",
FullName = "Nick Chapsas",
DateOfBirth = new DateTime(1993, 01, 01),
GitHubUsername = "nickchapsas"
};
var createCustomerHttpResponse = await _client.PostAsJsonAsync("customers", request);
var createdCustomer = await createCustomerHttpResponse.Content.ReadFromJsonAsync<CustomerResponse>();
// Act
var response = await _client.GetAsync($"customers/{createdCustomer!.Id}");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var customerResponse = await response.Content.ReadFromJsonAsync<CustomerResponse>();
customerResponse.Should().BeEquivalentTo(createdCustomer);
// Cleanup
_idsToDelete.Add(customerResponse!.Id);
}
Exercise: Write the following tests
The tests we need are:
Create_ShouldReturnBadRequest_WhenTheEmailIsInvalid
(The object used for bad requests isValidationProblemDetails
)GetAll_ShouldReturnAllCustomers_WhenCustomersExist
(One customer is enough. The object used isGetAllCustomersResponse
)Get_ShouldReturnNotFound_WhenCustomerDoesNotExist
Update_ShouldUpdateCustomerDetails_WhenDetailsAreValid
Delete_ShouldDeleteCustomer_WhenCustomerExists
Delete_ShouldReturnNotFound_WhenCustomerDoesNotExist
Solutions
Only expand the solutions if you are stuck. You are highly encouraged to try and write the tests yourself. Practice makes perfect and you only learn by doing.
Create_ShouldReturnBadRequest_WhenTheEmailIsInvalid
[Fact]
public async Task Create_ShouldReturnBadRequest_WhenTheEmailIsInvalid()
{
// Arrange
var request = new CustomerRequest
{
Email = "nick",
FullName = "Nick Chapsas",
DateOfBirth = new DateTime(1993, 01, 01),
GitHubUsername = "nickchapsas"
};
// Act
var response = await _client.PostAsJsonAsync("customers", request);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
var problemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
problemDetails!.Errors["Email"].Should().Equal("nick is not a valid email address");
}
GetAll_ShouldReturnAllCustomers_WhenCustomersExist
[Fact]
public async Task GetAll_ShouldReturnAllCustomers_WhenCustomersExist()
{
// Arrange
var request = new CustomerRequest
{
Email = "[email protected]",
FullName = "Nick Chapsas",
DateOfBirth = new DateTime(1993, 01, 01),
GitHubUsername = "nickchapsas"
};
var createCustomerHttpResponse = await _client.PostAsJsonAsync("customers", request);
var createdCustomer = await createCustomerHttpResponse.Content.ReadFromJsonAsync<CustomerResponse>();
_idsToDelete.Add(createdCustomer!.Id);
// Act
var response = await _client.GetAsync("customers");
// Assert
var customerResponse = await response.Content.ReadFromJsonAsync<GetAllCustomersResponse>();
response.StatusCode.Should().Be(HttpStatusCode.OK);
customerResponse!.Customers.Should().ContainEquivalentOf(createdCustomer).And.HaveCount(1);
}
Get_ShouldReturnNotFound_WhenCustomerDoesNotExist
[Fact]
public async Task Get_ShouldReturnNotFound_WhenCustomerDoesNotExist()
{
// Arrange
var customerId = Guid.NewGuid();
// Act
var response = await _client.GetAsync($"customers/{customerId}");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}
Update_ShouldUpdateCustomerDetails_WhenDetailsAreValid
[Fact]
public async Task Update_ShouldUpdateCustomerDetails_WhenDetailsAreValid()
{
// Arrange
var createRequest = new CustomerRequest
{
Email = "[email protected]",
FullName = "Nick Chapsas",
DateOfBirth = new DateTime(1993, 01, 01),
GitHubUsername = "nickchapsas"
};
var createCustomerHttpResponse = await _client.PostAsJsonAsync("customers", createRequest);
var createdCustomer = await createCustomerHttpResponse.Content.ReadFromJsonAsync<CustomerResponse>();
_idsToDelete.Add(createdCustomer!.Id);
var updateRequest = new CustomerRequest
{
Email = "[email protected]",
FullName = "Nick Chapsas",
DateOfBirth = new DateTime(1993, 01, 01),
GitHubUsername = "nickchapsas"
};
// Act
var response = await _client.PutAsJsonAsync($"customers/{createdCustomer!.Id}", updateRequest);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
var customerResponse = await response.Content.ReadFromJsonAsync<CustomerResponse>();
customerResponse.Should().BeEquivalentTo(updateRequest);
}
Delete_ShouldDeleteCustomer_WhenCustomerExists
[Fact]
public async Task Delete_ShouldDeleteCustomer_WhenCustomerExists()
{
// Arrange
var createRequest = new CustomerRequest
{
Email = "[email protected]",
FullName = "Nick Chapsas",
DateOfBirth = new DateTime(1993, 01, 01),
GitHubUsername = "nickchapsas"
};
var createCustomerHttpResponse = await _client.PostAsJsonAsync("customers", createRequest);
var createdCustomer = await createCustomerHttpResponse.Content.ReadFromJsonAsync<CustomerResponse>();
// Act
var response = await _client.DeleteAsync($"customers/{createdCustomer!.Id}");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
Delete_ShouldReturnNotFound_WhenCustomerDoesNotExist
[Fact]
public async Task Delete_ShouldReturnNotFound_WhenCustomerDoesNotExist()
{
// Arrange
var customerId = Guid.NewGuid();
// Act
var response = await _client.DeleteAsync($"customers/{customerId}");
// Assert
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
}