Jacob Shafi Jacob Shafi - 1 month ago 5
PHP Question

Why am I getting a route undefined error when testing my controller?

I am trying to internally test an API by calling my controller actions in my unit tests. For some reason I am getting InvalidArgumentException: Route [ListController@create] not defined. Can anyone help?

ListControllerTest.php

use Guzzle\Tests\GuzzleTestCase,
Guzzle\Plugin\Mock\MockPlugin,
Guzzle\Http\Message\Response,
Guzzle\Http\Client,
Guzzle\Service\Client as ServiceClient,
Guzzle\Http\EntityBody;

class ListTest extends TestCase {
public function testCreateList()
{
$user_id = getTestReceiverUserId();
$response = $this->action(
'POST',
'ListController@create',
[
'body' => array(
"name" => "open,shared",
"users" => array(array("user_id" => $user_id)),
"visibility" => "2",
"joinability" => "2",
"adminship" => "1",
"color" => "#2bc069"
),
'headers' => array('Auth' => getTestAuthSender())
]
);
}


routes.php

Route::group(array('namespace' => 'App\Controllers\v2', 'prefix' => '2.0'), function()
{
Route::controller('user', 'UserController');
Route::controller('invite', 'InviteController');
Route::controller('list', 'ListController');

Route::post('/login','AuthController@login');
Route::any('/logout','AuthController@logout');
});


ListController.php

namespace App\Controllers\v1;
use BaseController;
use Timothylhuillier\LaravelMixpanel\Facades\LaravelMixpanel;
class ListController extends BaseController
{
public function postCreate()
{
Some Code
}

Answer

I would imagine that it's because you have have your ListController inside your v2 namespace.

Try changing:

'ListController@create',

to:

'v2\ListController@postCreate',

Hope this helps!

Comments