仅当访问 Laravel 5 中的根页面( visit('/') )时,PHPUnit 测试才会失败

Posted

技术标签:

【中文标题】仅当访问 Laravel 5 中的根页面( visit(\'/\') )时,PHPUnit 测试才会失败【英文标题】:PHPUnit testing FAILS ONLY when visiting the root page( visit('/') ) in Laravel 5仅当访问 Laravel 5 中的根页面( visit('/') )时,PHPUnit 测试才会失败 【发布时间】:2016-10-28 20:21:30 【问题描述】:

我在 docker 容器 dockervel 中运行 Laravel 应用程序,并且

测试

<?php
use Illuminate\Foundation\Testing\WithoutMiddleware ;
use Illuminate\Foundation\Testing\DatabaseMigrations ;
use Illuminate\Foundation\Testing\DatabaseTransactions ;
use App\User1;

class MyTest extends TestCase


    public function testExample()
    
        $this->assertTrue(true);
    
     public function providerAllUrisWithResponseCode()
    
        return [
            ['/', 200],
            ['/thank', 200],
            ['/non-existing', 404],
        ];
    
    public function testDisplayThankYou($value='')
    
        $this->visit('/thankyou')
        ->see('THank you!');

    

    public function testNewUserRegistration()
    
        $this->visit('/')
         ->type('Taylor', 'name')
         ->type('taylor@talor.com', 'email')         
         ->press('Register')
         ->seePageIs('/thankyou');
    

    public function testPageControllerGet()
    
        $this->call('GET', '/');
    

    public function testPageControllerGettoThankYou()
    
        $this->call('GET', 'thankyou');
    

    public function testUser1ControllerPost()
    
        $response= $this->call('POST', '/');
    



只有当 $this->visit('/') 显示方法 testNewUserRegistration 时它才会失败:

There was 1 failure:

1) MyTest::testDisplayWelcome
A request to [http://localhost] failed. Received status code [500].

Caused by
exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown mysql server host 'mysql' (2)' in ..vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55

注意:可以通过我的应用进入数据库。

laravel.log:

[2016-06-27 09:02:53] testing.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql' (2)' in ../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
Stack trace:
#0 ../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(55): PDO->__construct('mysql:host=mysq...', 'homestead', 'secret', Array)
#1 ../vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=mysq...', Array, Array)
#2 ../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(61): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#3 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\closure()
#4 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(908): call_user_func(Object(Closure))
#5 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(776): Illuminate\Database\Connection->getPdo()
#6 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(661): Illuminate\Database\Connection->reconnectIfMissingConnection()
#7 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(342): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#8 ../vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1583): Illuminate\Database\Connection->select('select * from `...', Array, true)
#9 ../vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1569): Illuminate\Database\Query\Builder->runSelect()
#10 ../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(624): Illuminate\Database\Query\Builder->get(Array)
#11 ../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(316): Illuminate\Database\Eloquent\Builder->getModels(Array)
#12 ../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(649): Illuminate\Database\Eloquent\Builder->get(Array)
#13 ../app/Http/Controllers/PageController.php(14): Illuminate\Database\Eloquent\Model::all()
#14 [internal function]: App\Http\Controllers\PageController->home()
#15 ../vendor/laravel/framework/src/Illuminate/Routing/Controller.php(80): call_user_func_array(Array, Array)
#16 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(146): Illuminate\Routing\Controller->callAction('home', Array)
#17 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(94): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\PageController), Object(Illuminate\Routing\Route), 'home')
#18 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#19 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#20 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#21 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#22 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(96): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#23 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\PageController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'home')
#24 ../vendor/laravel/framework/src/Illuminate/Routing/Route.php(174): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'home')
#25 ../vendor/laravel/framework/src/Illuminate/Routing/Route.php(140): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
#26 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(724): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#27 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#28 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#29 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#30 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#32 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#33 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#34 ../vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#35 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#36 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#37 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#38 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#39 ../vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#40 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#42 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#43 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#44 ../vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#45 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#47 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#48 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#49 ../vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#50 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#51 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#52 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#53 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#54 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#55 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#56 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#57 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#58 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#59 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#60 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\closure(Object(Illuminate\Http\Request))
#61 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#62 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#63 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#64 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#65 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#66 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#67 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#68 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#69 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#70 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#71 ../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(515): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#72 ../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php(78): Illuminate\Foundation\Testing\TestCase->call('GET', 'http://localhos...', Array, Array, Array)
#73 ../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php(61): Illuminate\Foundation\Testing\TestCase->makeRequest('GET', '/')
#74 ../tests/MyTest.php(38): Illuminate\Foundation\Testing\TestCase->visit('/')
#75 [internal function]: MyTest->testNewUserRegistration()
#76 ../vendor/phpunit/phpunit/src/Framework/TestCase.php(909): ReflectionMethod->invokeArgs(Object(MyTest), Array)
#77 ../vendor/phpunit/phpunit/src/Framework/TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
#78 ../vendor/phpunit/phpunit/src/Framework/TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
#79 ../vendor/phpunit/phpunit/src/Framework/TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(MyTest))
#80 ../vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#81 ../vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#82 ../vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#83 ../vendor/phpunit/phpunit/src/TextUI/Command.php(149): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#84 ../vendor/phpunit/phpunit/src/TextUI/Command.php(100): PHPUnit_TextUI_Command->run(Array, true)
#85 ../vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
#86 main  
[2016-06-27 09:03:05] testing.ERROR: exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host 'mysql' (2)' in ../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:55
Stack trace:
#0 ../vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php(55): PDO->__construct('mysql:host=mysq...', 'homestead', 'secret', Array)
#1 ../vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php(24): Illuminate\Database\Connectors\Connector->createConnection('mysql:host=mysq...', Array, Array)
#2 ../vendor/laravel/framework/src/Illuminate/Database/Connectors/ConnectionFactory.php(61): Illuminate\Database\Connectors\MySqlConnector->connect(Array)
#3 [internal function]: Illuminate\Database\Connectors\ConnectionFactory->Illuminate\Database\Connectors\closure()
#4 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(908): call_user_func(Object(Closure))
#5 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(776): Illuminate\Database\Connection->getPdo()
#6 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(661): Illuminate\Database\Connection->reconnectIfMissingConnection()
#7 ../vendor/laravel/framework/src/Illuminate/Database/Connection.php(342): Illuminate\Database\Connection->run('select * from `...', Array, Object(Closure))
#8 ../vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1583): Illuminate\Database\Connection->select('select * from `...', Array, true)
#9 ../vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(1569): Illuminate\Database\Query\Builder->runSelect()
#10 ../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(624): Illuminate\Database\Query\Builder->get(Array)
#11 ../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(316): Illuminate\Database\Eloquent\Builder->getModels(Array)
#12 ../vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(649): Illuminate\Database\Eloquent\Builder->get(Array)
#13 ../app/Http/Controllers/PageController.php(14): Illuminate\Database\Eloquent\Model::all()
#14 [internal function]: App\Http\Controllers\PageController->home()
#15 ../vendor/laravel/framework/src/Illuminate/Routing/Controller.php(80): call_user_func_array(Array, Array)
#16 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(146): Illuminate\Routing\Controller->callAction('home', Array)
#17 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(94): Illuminate\Routing\ControllerDispatcher->call(Object(App\Http\Controllers\PageController), Object(Illuminate\Routing\Route), 'home')
#18 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#19 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#20 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#21 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#22 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(96): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#23 ../vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(54): Illuminate\Routing\ControllerDispatcher->callWithinStack(Object(App\Http\Controllers\PageController), Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'home')
#24 ../vendor/laravel/framework/src/Illuminate/Routing/Route.php(174): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request), 'App\\Http\\Contro...', 'home')
#25 ../vendor/laravel/framework/src/Illuminate/Routing/Route.php(140): Illuminate\Routing\Route->runController(Object(Illuminate\Http\Request))
#26 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(724): Illuminate\Routing\Route->run(Object(Illuminate\Http\Request))
#27 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#28 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#29 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#30 [internal function]: Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#31 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#32 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#33 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#34 ../vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#35 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#36 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#37 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#38 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#39 ../vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#40 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#42 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#43 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#44 ../vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#45 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#46 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#47 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#48 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#49 ../vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#50 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#51 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#52 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#53 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#54 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#55 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#56 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(726): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#57 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(699): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#58 ../vendor/laravel/framework/src/Illuminate/Routing/Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#59 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#60 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\closure(Object(Illuminate\Http\Request))
#61 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#62 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#63 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#64 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(136): call_user_func_array(Array, Array)
#65 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\closure(Object(Illuminate\Http\Request))
#66 ../vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#67 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\closure(Object(Illuminate\Http\Request))
#68 ../vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#69 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#70 ../vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#71 ../vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php(515): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#72 ../tests/MyTest.php(47): Illuminate\Foundation\Testing\TestCase->call('GET', '/')
#73 [internal function]: MyTest->testPageControllerGet()
#74 ../vendor/phpunit/phpunit/src/Framework/TestCase.php(909): ReflectionMethod->invokeArgs(Object(MyTest), Array)
#75 ../vendor/phpunit/phpunit/src/Framework/TestCase.php(768): PHPUnit_Framework_TestCase->runTest()
#76 ../vendor/phpunit/phpunit/src/Framework/TestResult.php(612): PHPUnit_Framework_TestCase->runBare()
#77 ../vendor/phpunit/phpunit/src/Framework/TestCase.php(724): PHPUnit_Framework_TestResult->run(Object(MyTest))
#78 ../vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#79 ../vendor/phpunit/phpunit/src/Framework/TestSuite.php(747): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#80 ../vendor/phpunit/phpunit/src/TextUI/TestRunner.php(440): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult))
#81 ../vendor/phpunit/phpunit/src/TextUI/Command.php(149): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#82 ../vendor/phpunit/phpunit/src/TextUI/Command.php(100): PHPUnit_TextUI_Command->run(Array, true)
#83 ../vendor/phpunit/phpunit/phpunit(47): PHPUnit_TextUI_Command::main()
#84 main  

我的: .env

DB_CONNECTION=mysql
    DB_HOST=mysql
    DB_PORT=3306
    DB_DATABASE=homestead
    DB_USERNAME=homestead
    DB_PASSWORD=secret

config/database.php

<?php
return [

    'fetch' => PDO::FETCH_CLASS,
    'default' => env('DB_CONNECTION', 'mysql'),
    'connections' => [

        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'mysql'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
        ],
    ],

    'migrations' => 'migrations',
    'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],
    ],
];

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory suffix="Test.php">./tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
            <exclude>
                <file>./app/Http/routes.php</file>
            </exclude>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_HOST" value="mysql"/>
        <env name="DB_USERNAME" value="homestead"/>
        <env name="DB_PASSWORD" value="secret"/>
    </php>
</phpunit>

请帮忙!

【问题讨论】:

您收到的状态码为500。检查您的日志以查看引发了什么错误。 @z3r0ck 以上更新了一个日志文件。 'mysql' =&gt; [ 'driver' =&gt; 'mysql', 'host' =&gt; env('DB_HOST', 'mysql'), 尝试将 .env 中的 DB_HOST 值更改为 localhost @z3r0ck 同样的事情正在发生。 但是当我改变 我得到这个异常 'PDOException' 并带有消息 'SQLSTATE[HY000] [2002] Can't connect to local MySQL通过 /home/zagorka/Desktop/docker/dockervel/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php 中的套接字 '/var/run/mysqld/mysqld.sock' (2)' 服务器:55 【参考方案1】:

尝试其他版本要容易得多。它基于 PHP 7,可以在一个命令中运行 phpunit 测试dunit

$ dstop $ git checkout builder $ . ./aliases.sh $ dup $ dunit

【讨论】:

这对 PhpUnits 有帮助!谢谢!但我想知道为什么它失败了,因为我在运行 Behat 时在日志中得到了同样的错误,问题就在这里:***.com/questions/38109339/…【参考方案2】:

试试这个: 查看mysql 容器是否已启动:docker ps 您应该会看到一个 mysql 容器。如果没有,则运行dstop & dup

【讨论】:

【参考方案3】:

我认为您的代码 sn-p 中发生了两件事。

第一件事是您将 host 定义为mysql,而不是 driver(我认为这是您要设置的那个)。尝试将环境变量DB_CONNECTION 设置为mysql 而不是DB_HOST

第二件事是你需要在你的测试类中添加DatabaseMigrations trait,尝试添加这个:

<?php

// Imports

class MyTest extends TestCase 

    use DatabaseMigrations; // This line
    ...


如果没有这个,你的测试可能会失败,因为它会找到数据库,但它不会找到在其中创建的任何表。

希望对你有帮助。

编辑

测试的最佳实践是模拟需要与数据库通信的依赖项,或者使用内存数据库进行测试,将它们与真实数据库分开。

为了将测试数据库与真实数据库分开,您可以在内存中使用sqlite 驱动程序。只需在 database.php 文件中更新您的 sqlite 驱动程序,如下所示:

'sqlite' => [
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
],

并在您的测试中将其用作DB_CONNECTION 环境变量(修改phpunit.xml):

<env name="DB_CONNECTION" value="sqlite"/>

【讨论】:

嗯,当添加 DatabaseMigration 时,每个测试方法都会发送一个与上面相同的错误,因为即使其中一些不需要它,它也会尝试连接到数据库。 这里的最佳实践可能是模拟所有需要与数据库通信的控制器依赖项,或者使用sqlite 驱动程序和内存中的数据库,这样你就不需要不得不担心数据库访问。我用 sqlite 配置修改了我的答案。 sqlite 更改没有帮助,同样的问题。

以上是关于仅当访问 Laravel 5 中的根页面( visit('/') )时,PHPUnit 测试才会失败的主要内容,如果未能解决你的问题,请参考以下文章

Nginx 总是将 Laravel 路由重定向到默认的根 index.php 页面

仅当需要字段时,Laravel 5.5 验证最小长度

如何在 Laravel 5 中访问刀片中的 URL 段?

在 laravel 5.3 中重定向回请求的页面

使用路由 Laravel 5.2 时访问资源时出错

Laravel 5.0 - 登录失败时更改重定向