Skip to main content

Exercise: The rest of the tests

Now it is your turn to practice. There are many tests that can be written for our website and you should take it upon your self to practice them even if they are not in the list below. Remember, practice makes perfect and you only learn by doing.

Write the following tests:

  • Get_ShouldReturnCustomer_WhenCustomerExists
  • GetAll_ShouldContainCustomer_WhenCustomerExists
  • Update_ShouldUpdateCustomer_WhenDataIsValid
  • Delete_ShouldDeleteCustomer_WhenCustomerExists
tip

If you need to Accept a browser-level dialog (like the one that appears when you delete a customer), you can use the following piece of code:

_page.Dialog += (_, dialog) => dialog.AcceptAsync();

Challenge yourselves to find more actions to test and only expand the solutions below if you are stuck and you need a little help.

Solutions

Get_ShouldReturnCustomer_WhenCustomerExists
GetCustomerTests.cs
[Fact]
public async Task Get_ShouldReturnCustomer_WhenCustomerExists()
{
// Arrange
var customer = _customerGenerator.Generate();
_testingContext.GitHubApiServer.SetupUser(customer.GitHubUsername);
await _customerRepository.CreateAsync(customer);

// Act
await _page.GotoAsync($"{TestingContext.AppUrl}/customer/{customer.Id}");

// Assert
var fullName = await _page.Locator("id=fullname-field").InnerTextAsync();
var email = await _page.Locator("id=email-field").InnerTextAsync();
var githubUsername = await _page.Locator("id=github-username-field").InnerTextAsync();
var dateOfBirth = await _page.Locator("id=dob-field").InnerTextAsync();

fullName.Should().Be(customer.FullName);
email.Should().Be(customer.Email);
githubUsername.Should().Be(customer.GitHubUsername);
dateOfBirth.Should().Be(customer.DateOfBirth.ToString("dd/MM/yyyy"));
}
GetAll_ShouldContainCustomer_WhenCustomerExists
GetAllCustomersTests.cs
[Fact]
public async Task GetAll_ShouldContainCustomer_WhenCustomerExists()
{
// Arrange
var customer = _customerGenerator.Generate();
_testingContext.GitHubApiServer.SetupUser(customer.GitHubUsername);
await _customerRepository.CreateAsync(customer);

// Act
await _page.GotoAsync($"{TestingContext.AppUrl}/customers");

// Assert
var tableRow = _page.Locator($"tr:has-text('{customer.Email}')");
var tableCells = tableRow.Locator("td");

var fullName = await tableCells.Nth(0).InnerTextAsync();
var email = await tableCells.Nth(1).InnerTextAsync();
var githubUsername = await tableCells.Nth(2).InnerTextAsync();
var dateOfBirth = await tableCells.Nth(3).InnerTextAsync();

fullName.Should().Be(customer.FullName);
email.Should().Be(customer.Email);
githubUsername.Should().Be(customer.GitHubUsername);
dateOfBirth.Should().Be(customer.DateOfBirth.ToString("dd/MM/yyyy"));
}
Update_ShouldUpdateCustomer_WhenDataIsValid
UpdateCustomerTests.cs
[Fact]
public async Task Update_ShouldUpdateCustomer_WhenDataIsValid()
{
// Arrange
var customer = _customerGenerator.Generate();
var newCustomer = _customerGenerator.Generate();
_testingContext.GitHubApiServer.SetupUser(customer.GitHubUsername);
await _customerRepository.CreateAsync(customer);

await _page.GotoAsync($"{TestingContext.AppUrl}/update-customer/{customer.Id}");

// Act
await _page.Locator("id=fullname").FillAsync(newCustomer.FullName);
await _page.Locator("text=Submit").ClickAsync();

// Assert
var updatedCustomer = await _customerRepository.GetAsync(customer.Id);
updatedCustomer!.FullName.Should().Be(newCustomer.FullName);
}
Delete_ShouldDeleteCustomer_WhenCustomerExists
DeleteCustomerTests.cs
[Fact]
public async Task Delete_ShouldDeleteCustomer_WhenCustomerExists()
{
// Arrange
var customer = _customerGenerator.Generate();
_testingContext.GitHubApiServer.SetupUser(customer.GitHubUsername);
await _customerRepository.CreateAsync(customer);

await _page.GotoAsync($"{TestingContext.AppUrl}/customers");
_page.Dialog += (_, dialog) => dialog.AcceptAsync();

// Act
await _page.Locator("text='Delete'").ClickAsync();

// Assert
var customerExists = await _customerRepository.GetAsync(customer.Id);
customerExists.Should().BeNull();
}