所以我正在尝试为这样的 Laravel 应用程序编写单元测试:
protected function setUp()
{
parent::setUp();
$this->disableMiddlewareForAllTests();
$this->withoutExceptionHandling();
}
public function test_login_badRequest()
{
$response = $this->call('post', '/login');
$response->assertOk();
$response->assertJson(['status' => 400]);
}
以上工作正常,并给出以下输出:
Test 'Tests\Unit\LoginTest::test_login_badRequest' started
Test 'Tests\Unit\LoginTest::test_login_badRequest' ended
Time: 69 ms, Memory: 12.00MB
OK (1 test, 2 assertions)
但接下来的测试是另一个故事:
public function test_login_goodRequest()
{
$response = $this->call('post', '/login', [
'email' => 'email@email.com',
'password' => 'P4ssw0rd'
]);
$response->assertOk();
$response->assertJson(['status' => 200]);
}
一旦测试在应用程序中命中这行代码:
/* Retrieve User associated with posted email address */
$user = $this->userModel
->where('email', $request->post('email'))
->get();
die('here');// <-- Happens in browser, does not happen in tests.
这是错误:
1) Tests\Unit\LoginTest::test_login_goodRequest
PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
/var/www/html/projects/laravel-template/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:68 ...
几天来我一直在努力解决这个问题,任何想法这里发生了什么,以及如何让那个模具在测试中打印?干杯。
-- Laradock, Docker, nginx, mysql, PHP 7.2.4-1+ubuntu16.04.1+deb.sury.org+1 (cli) (build: Apr 5 2018 08:53:57) (NTS) 版权所有 (c) 1997-2018 PHP Group Zend Engine v3.2.0,版权所有 (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.4-1+ubuntu16.04.1+deb.sury.org+1,版权所有 (c) 1999-2018,通过 Zend 技术
编辑:
.env:
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:Z+p6uA3G2DyCCFBXOPgrWwls1U6z/vi8Zi2r4eKHxEY=
APP_DEBUG=true
DEBUGBAR_ENABLED=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=sample_database
DB_USERNAME=root
DB_PASSWORD=root
BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"