【问题标题】:How to check routes with unit testing on Laravel5如何在 Laravel5 上通过单元测试检查路线
【发布时间】:2016-02-03 08:36:55
【问题描述】:

我尝试使用单元测试,但它不起作用。不知何故,收到的状态码是 404。然后,终端说明原因是异常 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException'。

Routes.php

Route::get('/', function() {
    return view('toppage')->with('contents', "");
});

ExampleTest.php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->visit('/')
             ->see('Where lane');
    }
}

nignx.conf(从文件中摘录一部分)

location / {
        index            index.html index.php;
    }

    location ~* /MAMP(.*)$ {
        root             /Applications/MAMP/bin;
        index            index.php;

        location ~ \.php$ {
            try_files        $uri =404;
            fastcgi_pass     unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI.sock;
            fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include          fastcgi_params;
        }
    }

【问题讨论】:

    标签: php unit-testing laravel laravel-5


    【解决方案1】:

    如果你想看看你是否可以访问该路由。最好验证该链接上的文字。

    试试这个:

    class WelcomeTest extends TestCase {
    
        /**
         * Test if the public index page returns the correct view
         * GET /
         */
    
        public function testWelcomePage()
        {
            $response = $this->call('GET', '/');
    
            // check if we're getting the default welcome page. 
            //you can replace this with something on your default route's <title> Html tag
    
            $this->assertRegexp('/<title>Welcome<\/title>/', $response->getContent());
    
        }
    
    }
    

    如果失败,请先尝试在浏览器中获取该站点。清除您的路线,缓存并仔细检查页面上是否确实存在“Where lane”文本。

    【讨论】:

    • 感谢您的建议。根据您的想法,我创建了一个测试并检查它是否有效!然后,我检查了代码,$this->visit ('/') 没有按预期工作。结果因 NotFoundHttpException 而失败。虽然我不明白具体原因,但我会根据你的想法创建测试用例。
    • 太棒了,你应该把你的路由改成这样的“Route::get('/', 'WelcomeController@index');”然后运行“php artisan route:cache”验证路由去正确的地方“ php artisan route:list ”我认为这应该使 $this->visit ('/') 也可以工作。告诉我进展如何。
    • 感谢您的额外建议。至少我没有在终端上输入这些命令。所以,如果解决了,我会发布答案。
    • 在验证路由后,我尝试了“$this->visit('/')”,但它返回失败和 404 作为返回码。但是代码“$this->get('/')”起作用了。以防万一,我向你报告了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多