Josh Young
Josh Young

Reputation: 1366

Named Routes Not Recognised in Method When Called From PHPUnit Test

When calling a Method from another class that contains a named route the route is not recognised

This PHPUnit test is to check that functionality of the system still meets requirements as the system is complicated.

Testing is run from a local environment (homestead vagrant)

I am calling the method form here:

$orderLines = (new EDIProcessController())->getChanges($order['ediOrder'], $order['partnerId']);

And the named route that is causing the issue:

$orderLine->item_url = route('warehouse.products.view', $itemDetails->id);

I get the following error:

InvalidArgumentException: Route [warehouse.products.view] not defined.

/home/vagrant/vr/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:305
/home/vagrant/vr/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:790
/home/vagrant/vr/app/Http/Controllers/Warehouse/EDI/SPS/EDIProcessController.php:1575
/home/vagrant/vr/vendor/laravel/framework/src/Illuminate/Support/Collection.php:861
/home/vagrant/vr/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:139
/home/vagrant/vr/app/Http/Controllers/Warehouse/EDI/SPS/EDIProcessController.php:1746

Route as defined in the routes file:

Route::get('/view/{id}', 'Warehouse\ProductController@viewProduct')->name('warehouse.products.view');

This route works fine when called from within Laravel app but will not work within PHPUnit test

Upvotes: 2

Views: 592

Answers (1)

Josh Young
Josh Young

Reputation: 1366

Finally found the answer

There is a issue with using require_once for sub route files that is caused due to multiple instances of route files not all getting the required files.

Here is a dicussion here that I eventually found

switching the require_once to require at all levels of our route files solves the issue

Upvotes: 4

Related Questions