【问题标题】:Testing laravel routes identify method type测试 laravel 路由识别方法类型
【发布时间】:2018-10-23 05:18:12
【问题描述】:

如果请求是 get 或 post,我如何获取 Laravel 路由?

我尝试使用以下方法测试我的 laravel 路线

public function testRoutes()
    {
        $app = app();
        $routes = $app->routes->getRoutes();


        /**
         * Test if mynamespace routes are redirected to login page if is not the login page
         */
        echo  PHP_EOL;

        foreach ($routes as $route) {

            if(strpos($route->getName(),'mynamespace::' ) !== false ) {

                $url = $route->uri;

                //$appURL = env('APP_URL') .'/';

                $response = $this->get($url);

                if((int)$response->status() !== 200 ){
                    echo   $url . ' (FAILED) did not return 200. The response is ' . $response->status();
                    $this->assertTrue(false);
                } else {
                    echo  $url . ' (success ?)';
                    $this->assertTrue(true);
                }
                echo  PHP_EOL;

            }
        }
    }

但我想暂时排除发帖请求

【问题讨论】:

    标签: laravel routes phpunit


    【解决方案1】:

    我们可以看到Route 类有一个属性$methods

    您的解决方案如下所示:

    if (in_array('POST', $route->methods)) continue;
    

    看看 Laravel 本身提供的测试可能会很有趣。测试响应测试的简单方法等等!

    Laravel testing.

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 2016-06-10
      • 2012-05-02
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多