QueryContentContains Assertion fails with "An unexpected failure occured"

Hello everyone!

I’ve been trying to write a test case for an application, which displays a login prompt on its indexAction.
The goal of this test case is to succeed when the account is not found. I am testing for an error message that’s there when I do it manually.
Here’s the code:

public function testIndexActionDisplaysErrorsWhenCredentialsInvalid(){
        $postData = [
            'username' => 'foo',
            'password' => 'bar'
        ];

        $this->dispatch('/', 'POST', $postData);
        $this->assertResponseStatusCode(200);
        $this->assertModuleName('application');
        $this->assertControllerName(IndexController::class); // as specified in router's controller name alias
        $this->assertControllerClass('IndexController');
        $this->assertMatchedRouteName('home');
        $this->assertQueryContentContains('.container #application .text-danger', 'An unexpected failure occured');
    }

This test case fails with

1) ApplicationTest\Controller\IndexControllerTest::testIndexActionDisplaysErrorsWhenCredentialsInvalid
Failed asserting node denoted by .container #application .text-danger CONTAINS content "Account not found", Contents: [An unexpected failure occurred]

C:\Users\js\Documents\Projekte\Management\vendor\laminas\laminas-test\src\PHPUnit\Controller\AbstractHttpControllerTestCase.php:776
C:\Users\js\Documents\Projekte\Management\vendor\laminas\laminas-test\src\PHPUnit\Controller\AbstractHttpControllerTestCase.php:793
C:\Users\js\Documents\Projekte\Management\module\Application\test\Controller\IndexControllerTest.php:51

FAILURES!

The twist is: Even when I assert that the content of the node is “An unexpected failure occured”, it still fails:

There was 1 failure:

1) ApplicationTest\Controller\IndexControllerTest::testIndexActionDisplaysErrorsWhenCredentialsInvalid
Failed asserting node denoted by .container #application .text-danger CONTAINS content "An unexpected failure occured", Contents: [An unexpected failure occurred]

C:\Users\js\Documents\Projekte\Management\vendor\laminas\laminas-test\src\PHPUnit\Controller\AbstractHttpControllerTestCase.php:776
C:\Users\js\Documents\Projekte\Management\vendor\laminas\laminas-test\src\PHPUnit\Controller\AbstractHttpControllerTestCase.php:793
C:\Users\js\Documents\Projekte\Management\module\Application\test\Controller\IndexControllerTest.php:51

FAILURES!
Tests: 1, Assertions: 7, Failures: 1.

The page itself is looking like this:

I already checked the selector for the element, so I am pretty certain that this is not the culprit.
Any help would be greatly appreciated!

Check the content itself with $this->getResponse()->getContent() if the expected element is present.

1 Like

I see. I am getting this HTML back (shortened it before and after the section):

<form method="POST" name="application" id="application"><div class="text-danger error">An unexpected failure occurred</div>

occurred with double r. :frowning_face:

There’s a hiccup in the authentication process I guess.

Thanks @froschdesign.