Why is rebuildContainer() required in tests to assert mails correctly?

Created on 25 July 2024, 3 months ago
Updated 12 August 2024, 2 months ago

Problem/Motivation

A thing that @Anybody and I have noticed is that, while the UI works in practice and monkey testing, the tests seem to require a frequent container rebuild via $this->rebuildContainer() in order to work.
There are two possible options here:

  1. We are failing to clear some required caches in the Form, a good location for oversights since they usually aren't cleared every few seconds by the end-user.
  2. The $this->getMails() function from the AssertMailTrait within the tests themselves is in need of a container rebuild every time in order to register the newly sent mails.

The latter option is likely to be the cause of the problem here, as the method does call the container without rebuilding it first:

protected function getMails(array $filter = []) {
    $captured_emails = $this->container->get('state')->get('system.test_mail_collector', []);
    ...
}

@Anybody also found this helpful article, which might help with taking a closer look at this problem:
Features of using the service container in tests in Drupal 9

Steps to reproduce

Remove $this->rebuildContainer() from a test and see that it fails now, even though the functionality currently works in practice.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany lrwebks Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024