1

I have a TableObject that sends an email in the afterSave-Event to notify the admin of a change. After the switch from cakephp-3 to cakephp-4 the test for this fails with this error message:

Cake\Routing\Exception\MissingRouteException: A route matching ... could not be found.

The exception occurs on a line in the email template where I build a link like this:

$this->Url->build([
    'prefix' => 'Partner',
    'controller' => 'orders',
    'action' => 'view',
    $order->id,
]);

I believe that the routes are not set up in the context of a test for a Tableobject and therefore the reverse routing is not working. (I only get the error when running the test, not when the email is sent in the app).

Is there a way I can load all routes in the test?

David Albrecht
  • 634
  • 1
  • 4
  • 15
  • Note: `'controller' => 'orders'` != `'controller' => 'Orders'` - casing matters in 4.x more than in 3.x as a lot of magic inflection has been removed for performance reasons. – mark Dec 01 '20 at 13:33
  • right, I will be carefull with that. It is not the root of the problem though. In the context of my test the RoutesCollection of the Router is simply empty. Do I have to load the routes explicitly in the test? – David Albrecht Dec 02 '20 at 09:30
  • 1
    $this->loadRoutes(); yes I had the same issue a few weeks back when upgrading some app. – mark Dec 02 '20 at 16:21
  • That`s it! If you post it as an answer, I will accept it. It solves the problem precisely. – David Albrecht Dec 04 '20 at 11:19

1 Answers1

4

Since 4.x the routes are not auto loaded anymore. You need to add

$this->loadRoutes(); 

into your setUp() or before the test run to actively load them now.

mark
  • 21,691
  • 3
  • 49
  • 71