Laravel 5/Codeception无法正确路由 [英] Laravel 5 / Codeception not routing correctly

查看:69
本文介绍了Laravel 5/Codeception无法正确路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用代码接收为控制器功能编写API测试用例,并且遇到了一个问题,其中似乎无法正确评估到控制器功能的路由,并且评估似乎有所不同.关于测试用例中的内容.

I'm trying to write an API test case for a controller function using codeception, and I'm hitting an issue where the route to the controller function does not appear to be evaluated correctly, and the evaluation seems to be different depending on what I have in my test case.

这是我的测试用例中的代码示例:

Here is a code sample from my test case:

use \ApiTester;

class CustomerRegisterCest
{
    // tests
    public function testGetRegister(ApiTester $I)
    {
        $I->sendGET('register');
        $I->seeResponseCodeIs(200);
    }

    public function testPostRegister(ApiTester $I)
    {
        $I->sendPOST('register', [
            // set the data in here
        ]);
        $I->seeResponseCodeIs(200);
    }

我有一个route.php文件,其中包含以下路由:

I have a routes.php file containing these routes:

Route::get('/', ['as' => 'home', 'uses' => 'HomeController@getIndex']);
Route::get('register', ['as' => 'getRegister', 'uses' =>'RegistrationController@getRegister']);
Route::post('register', ['as' => 'postRegister', 'uses' => 'RegistrationController@postRegister']);

我已经在控制器类中插入了一些调试语句,以便可以看到运行了哪些路由,如下所示:

I have inserted some debug statements into my controller classes so that I can see what routes get run, like this:

    Log::debug('GET register');  // or GET index or POST register, etc

此刻,我从控制器类中剥离了所有内容,以便仅包含调试语句.

At the moment I have stripped down everything from my controller classes so that ONLY the debug statements are included.

按上述方式运行测试用例时,将获得以下调试输出:

When I run the test case as above, I get the following debug output:

GET register
GET index

...因此似乎sendPOST('register',...)实际上路由到"/"的GET路由,而不是"/register"的POST路由.在测试用例之外,一切都可以正常工作-我可以将POST路由到寄存器中,路由似乎可以正常工作,问题只出现在代码接收测试用例中.

... so it appears that sendPOST('register', ...) actually routes to the GET route for "/" instead of the POST route for "/register". Outside of the test case everything works normally -- I can POST to the register routes fine, routing appears to work OK, the problem only appears inside a codeception test case.

如果更改测试用例,以便在同一个函数调用中执行sendGET和sendPOST,例如:

If I change the test case so that I am doing the sendGET and the sendPOST inside the same function call, for example like this:

    // tests
    public function testPostRegister(ApiTester $I)
    {
        $I->sendGET('register');
        $I->seeResponseCodeIs(200);
        $I->sendPOST('register', [
            // set the data in here
        ]);
        $I->seeResponseCodeIs(200);
    }

然后我看到以下调试输出:

then I see this debug output:

GET register
GET register

...因此,通过将sendGET插入与sendPOST相同的函数中,它已更改了sendPOST行为,因此现在它路由到用于注册的GET路由,而不是用于索引的GET路由(但仍然不会路由到正确的POST路由.

... so that by inserting the sendGET into the same function as the sendPOST, it has changed the sendPOST behaviour so that it now routes to the GET route for register instead of the GET route for index (but still won't route to the correct POST route).

我尝试打开xdebug,但从xdebug输出中看不到任何线索.

I have tried turning xdebug on and don't have any clues from the xdebug output as to what's going on either.

推荐答案

我想我经过大量命令行调试(使用phpstorm)后找到了答案:

I think I found the answer after a lot of command line debugging (using phpstorm):

像这样声明控制器中的POST寄存器路由处理功能:

The POST register route handling function in the controller was declared like this:

public function postRegister(RegistrationRequest $request)
{

...要求通过依赖项注入传递Request的实例.该请求包含一些验证代码,如果由于某种原因验证代码无法完成(例如引发异常),则控制器函数将永远不会被调用-因为构建请求失败.

... requiring an instance of Request to be passed in via dependency injection. That request contained some validation code and if for some reason the validation code could not complete (e.g. throws an exception) then the controller function never gets called -- because building the request fails.

在浏览器范围内,这将引发500错误,但在代码接收范围内,异常被捕获的方式有所不同,并且它返回到/的重定向(不包含任何数据).这一切都发生在控制器功能之外而不是内部,因此控制器功能中的Log语句永远不会运行,因为永远不会调用该函数.代码接收中的异常处理程序是一个通用陷阱.

This, in browser-land, throws a 500 error but in codeception land the exception is trapped differently and it returns a redirect to / with no data. This all happens outside of the controller function rather than inside it, so that the Log statement in the controller function never runs because the function never gets called. The exception handler in codeception is a generic trap.

隐含的建议是,在控制器中进行依赖项注入可能不是一个好主意.或者,也许通用异常处理程序是个坏主意.

The implicit suggestion is that maybe dependency injections in controllers are a bad idea. Or, maybe, that generic exception handlers are a bad idea.

这篇关于Laravel 5/Codeception无法正确路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆