【问题标题】:Why am I getting a route undefined error when testing my controller?为什么在测试我的控制器时会出现路由未定义错误?
【发布时间】:2016-10-31 20:52:27
【问题描述】:

我正在尝试通过在单元测试中调用我的控制器操作来内部测试 API。出于某种原因,我收到 InvalidArgumentException: Route [ListController@create] not defined。任何人都可以帮忙吗?

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())
            ]
        );
    }
路由.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');
});
列表控制器.php
namespace App\Controllers\v1;
use BaseController;
use Timothylhuillier\LaravelMixpanel\Facades\LaravelMixpanel;
class ListController extends BaseController
{
    public function postCreate()
    {
       Some Code
    }

【问题讨论】:

  • 你使用的是什么版本的 Laravel?
  • 我使用的是 4.2 版
  • @RossWilson 你知道问题是什么吗?

标签: php api unit-testing phpunit


【解决方案1】:

我想这是因为您的 ListController 在您的 v2 命名空间中。

尝试改变:

'ListController@create',

到:

'v2\ListController@postCreate',

希望这会有所帮助!

【讨论】:

  • 感谢您的快速响应,但不幸的是我遇到了同样的错误。我什至尝试在命名空间前面加上我的路由在测试中看起来像这样......'App\Controllers\v2\ListController@create'。 :(
  • @JacobShafi 我已经更新了我的答案。看看是不是这样。
  • 是的!这完美!非常感谢!我运行了“php artisan routes”来查看完整的路线,而您建议的路线就是其中之一。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多