Home>

If there is no specific value in the controller, there is a place that redirects with a flash message

I want to check the flash that should return if there is no variable value in this part

Error message
Session content for "Flash.flash.0.message" differs.
Failed asserting that null matches expected 'Invalid request.'.</Code></pre>
<strong>Applicable source code</strong>
<pre><code>public function testPayCheckSalonId ($Id = null)
    {
        // Can error message and reductive processing be done only when there is no
        $this->enableRetainFlashMessages ();
        $this->post ('/ payments/pay /'. $Id);
        $this->assertSession ('Invalid request.', 'Flash.flash.0.message');
    }
Original controller
public function pay ($Id = null) {
    if (empty ($Id)) {
      $this->Flash->error (__ ('Invalid request.'));
      return $this->redirect ($this->referer ());
    }
}

I also rewritten the following format

$Id = '';
        if (empty ($Id)) {
            $this->enableRetainFlashMessages ();
            $this->post ('/ payments/pay /'. $Id);
            $this->assertSession ('Invalid request.', 'Flash.flash.0.message');
        }


This is the same error as the first

$Id = '';
        if (empty ($Id)) {
            $this->enableRetainFlashMessages ();
            $this->post ('/ payments/pay /'. $Id);
            $this->assertFlashElement ('Flash/error');
        }


I was angry that there was no method here
Error: Call to undefined method App \ Test \ TestCase \ Controller \ PaymentsControllerTest :: assertFlashElement ()

How can I check if a flash message has been issued?

Supplemental information (FW/tool version etc.)

cakephp version 3.5
Execution environment local (MAMP)

  • Answer # 1

    Because this method must be a logged-in user, the request was sent without having that information in the session, so the flash message of the target location was not reached